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"
29 #include "wx/html/htmlwin.h"
30 #include "wx/html/htmlproc.h"
31 #include "wx/clipbrd.h"
32 #include "wx/dataobj.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/listimpl.cpp"
39 // ----------------------------------------------------------------------------
40 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
41 // events when a captured mouse is held outside the window
42 // ----------------------------------------------------------------------------
44 class wxHtmlWinAutoScrollTimer
: public wxTimer
47 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
48 wxEventType eventTypeToSend
,
52 m_eventType
= eventTypeToSend
;
57 virtual void Notify();
60 wxScrolledWindow
*m_win
;
61 wxEventType m_eventType
;
65 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
68 void wxHtmlWinAutoScrollTimer::Notify()
70 // only do all this as long as the window is capturing the mouse
71 if ( wxWindow::GetCapture() != m_win
)
75 else // we still capture the mouse, continue generating events
77 // first scroll the window if we are allowed to do it
78 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
79 event1
.SetEventObject(m_win
);
80 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
82 // and then send a pseudo mouse-move event to refresh the selection
83 wxMouseEvent
event2(wxEVT_MOTION
);
84 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
86 // the mouse event coordinates should be client, not screen as
87 // returned by wxGetMousePosition
88 wxWindow
*parentTop
= m_win
;
89 while ( parentTop
->GetParent() )
90 parentTop
= parentTop
->GetParent();
91 wxPoint ptOrig
= parentTop
->GetPosition();
92 event2
.m_x
-= ptOrig
.x
;
93 event2
.m_y
-= ptOrig
.y
;
95 event2
.SetEventObject(m_win
);
97 // FIXME: we don't fill in the other members - ok?
98 m_win
->GetEventHandler()->ProcessEvent(event2
);
100 else // can't scroll further, stop
107 #endif // wxUSE_CLIPBOARD
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 // item of history list
116 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
119 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
120 int GetPos() const {return m_Pos
;}
121 void SetPos(int p
) {m_Pos
= p
;}
122 const wxString
& GetPage() const {return m_Page
;}
123 const wxString
& GetAnchor() const {return m_Anchor
;}
132 //-----------------------------------------------------------------------------
133 // our private arrays:
134 //-----------------------------------------------------------------------------
136 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
137 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
)
139 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
140 WX_DEFINE_LIST(wxHtmlProcessorList
)
142 //-----------------------------------------------------------------------------
143 // wxHtmlWindowMouseHelper
144 //-----------------------------------------------------------------------------
146 wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface
*iface
)
147 : m_tmpMouseMoved(false),
154 void wxHtmlWindowMouseHelper::HandleMouseMoved()
156 m_tmpMouseMoved
= true;
159 bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell
*rootCell
,
161 const wxMouseEvent
& event
)
166 wxHtmlCell
*cell
= rootCell
->FindCellByPos(pos
.x
, pos
.y
);
167 // this check is needed because FindCellByPos returns terminal cell and
168 // containers may have empty borders -- in this case NULL will be
173 // adjust the coordinates to be relative to this cell:
174 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
176 return OnCellClicked(cell
, relpos
.x
, relpos
.y
, event
);
179 void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell
*rootCell
,
182 wxHtmlCell
*cell
= rootCell
? rootCell
->FindCellByPos(pos
.x
, pos
.y
) : NULL
;
184 if (cell
!= m_tmpLastCell
)
186 wxHtmlLinkInfo
*lnk
= NULL
;
189 // adjust the coordinates to be relative to this cell:
190 wxPoint relpos
= pos
- cell
->GetAbsPos(rootCell
);
191 lnk
= cell
->GetLink(relpos
.x
, relpos
.y
);
196 cur
= cell
->GetMouseCursor(m_interface
);
198 cur
= m_interface
->GetHTMLCursor(
199 wxHtmlWindowInterface::HTMLCursor_Default
);
201 m_interface
->GetHTMLWindow()->SetCursor(cur
);
203 if (lnk
!= m_tmpLastLink
)
206 m_interface
->SetHTMLStatusText(lnk
->GetHref());
208 m_interface
->SetHTMLStatusText(wxEmptyString
);
213 m_tmpLastCell
= cell
;
215 else // mouse moved but stayed in the same cell
219 OnCellMouseHover(cell
, pos
.x
, pos
.y
);
223 m_tmpMouseMoved
= false;
226 bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell
*cell
,
227 wxCoord x
, wxCoord y
,
228 const wxMouseEvent
& event
)
230 wxCHECK_MSG( cell
, false, _T("can't be called with NULL cell") );
232 return cell
->ProcessMouseClick(m_interface
, wxPoint(x
, y
), event
);
235 void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 wxList
wxHtmlWindow::m_Filters
;
247 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
248 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
249 wxCursor
*wxHtmlWindow::ms_cursorLink
= NULL
;
250 wxCursor
*wxHtmlWindow::ms_cursorText
= NULL
;
252 void wxHtmlWindow::CleanUpStatics()
254 wxDELETE(m_DefaultFilter
);
255 WX_CLEAR_LIST(wxList
, m_Filters
);
256 if (m_GlobalProcessors
)
257 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
258 wxDELETE(m_GlobalProcessors
);
259 wxDELETE(ms_cursorLink
);
260 wxDELETE(ms_cursorText
);
263 void wxHtmlWindow::Init()
265 m_tmpCanDrawLocks
= 0;
266 m_FS
= new wxFileSystem();
268 m_RelatedStatusBar
= -1;
269 #endif // wxUSE_STATUSBAR
270 m_RelatedFrame
= NULL
;
271 m_TitleFormat
= wxT("%s");
272 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
274 m_Parser
= new wxHtmlWinParser(this);
275 m_Parser
->SetFS(m_FS
);
278 m_History
= new wxHtmlHistoryArray
;
283 m_makingSelection
= false;
285 m_timerAutoScroll
= NULL
;
286 m_lastDoubleClick
= 0;
287 #endif // wxUSE_CLIPBOARD
289 m_eraseBgInOnPaint
= false;
290 m_tmpSelFromCell
= NULL
;
293 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
294 const wxPoint
& pos
, const wxSize
& size
,
295 long style
, const wxString
& name
)
297 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
298 style
| wxVSCROLL
| wxHSCROLL
,
303 SetPage(wxT("<html><body></body></html>"));
308 wxHtmlWindow::~wxHtmlWindow()
312 #endif // wxUSE_CLIPBOARD
321 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
333 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
335 m_RelatedFrame
= frame
;
336 m_TitleFormat
= format
;
342 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
344 m_RelatedStatusBar
= bar
;
346 #endif // wxUSE_STATUSBAR
350 void wxHtmlWindow::SetFonts(const wxString
& normal_face
, const wxString
& fixed_face
, const int *sizes
)
352 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
354 // re-layout the page after changing fonts:
355 DoSetPage(*(m_Parser
->GetSource()));
358 void wxHtmlWindow::SetStandardFonts(int size
,
359 const wxString
& normal_face
,
360 const wxString
& fixed_face
)
362 m_Parser
->SetStandardFonts(size
, normal_face
, fixed_face
);
364 // re-layout the page after changing fonts:
365 DoSetPage(*(m_Parser
->GetSource()));
368 bool wxHtmlWindow::SetPage(const wxString
& source
)
370 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
371 return DoSetPage(source
);
374 bool wxHtmlWindow::DoSetPage(const wxString
& source
)
376 wxString
newsrc(source
);
378 wxDELETE(m_selection
);
380 // we will soon delete all the cells, so clear pointers to them:
381 m_tmpSelFromCell
= NULL
;
383 // pass HTML through registered processors:
384 if (m_Processors
|| m_GlobalProcessors
)
386 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
390 nodeL
= m_Processors
->GetFirst();
391 if ( m_GlobalProcessors
)
392 nodeG
= m_GlobalProcessors
->GetFirst();
394 // VS: there are two lists, global and local, both of them sorted by
395 // priority. Since we have to go through _both_ lists with
396 // decreasing priority, we "merge-sort" the lists on-line by
397 // processing that one of the two heads that has higher priority
398 // in every iteration
399 while (nodeL
|| nodeG
)
401 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
402 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
405 if (nodeL
->GetData()->IsEnabled())
406 newsrc
= nodeL
->GetData()->Process(newsrc
);
407 nodeL
= nodeL
->GetNext();
411 if (nodeG
->GetData()->IsEnabled())
412 newsrc
= nodeG
->GetData()->Process(newsrc
);
413 nodeG
= nodeG
->GetNext();
418 // ...and run the parser on it:
419 wxClientDC
*dc
= new wxClientDC(this);
420 dc
->SetMapMode(wxMM_TEXT
);
421 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
422 SetBackgroundImage(wxNullBitmap
);
430 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
432 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
433 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
435 if (m_tmpCanDrawLocks
== 0)
440 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
442 return DoSetPage(*(GetParser()->GetSource()) + source
);
445 bool wxHtmlWindow::LoadPage(const wxString
& location
)
447 wxBusyCursor busyCursor
;
451 bool needs_refresh
= false;
454 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
456 // store scroll position into history item:
458 GetViewStart(&x
, &y
);
459 (*m_History
)[m_HistoryPos
].SetPos(y
);
462 if (location
[0] == wxT('#'))
465 wxString anch
= location
.Mid(1) /*1 to end*/;
467 rt_val
= ScrollToAnchor(anch
);
470 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
472 wxString anch
= location
.AfterFirst(wxT('#'));
474 rt_val
= ScrollToAnchor(anch
);
477 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
478 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
480 wxString anch
= location
.AfterFirst(wxT('#'));
482 rt_val
= ScrollToAnchor(anch
);
488 needs_refresh
= true;
491 if (m_RelatedStatusBar
!= -1)
493 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
496 #endif // wxUSE_STATUSBAR
498 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
500 // try to interpret 'location' as filename instead of URL:
503 wxFileName
fn(location
);
504 wxString location2
= wxFileSystem::FileNameToURL(fn
);
505 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
510 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
517 wxList::compatibility_iterator node
;
518 wxString src
= wxEmptyString
;
521 if (m_RelatedStatusBar
!= -1)
523 wxString msg
= _("Loading : ") + location
;
524 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
527 #endif // wxUSE_STATUSBAR
529 node
= m_Filters
.GetFirst();
532 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
535 src
= h
->ReadFile(*f
);
538 node
= node
->GetNext();
540 if (src
== wxEmptyString
)
542 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
543 src
= m_DefaultFilter
->ReadFile(*f
);
546 m_FS
->ChangePathTo(f
->GetLocation());
547 rt_val
= SetPage(src
);
548 m_OpenedPage
= f
->GetLocation();
549 if (f
->GetAnchor() != wxEmptyString
)
551 ScrollToAnchor(f
->GetAnchor());
557 if (m_RelatedStatusBar
!= -1)
558 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
559 #endif // wxUSE_STATUSBAR
563 if (m_HistoryOn
) // add this page to history there:
565 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
567 if (m_HistoryPos
< 0 ||
568 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
569 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
572 for (int i
= 0; i
< c
; i
++)
573 m_History
->RemoveAt(m_HistoryPos
);
574 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
578 if (m_OpenedPageTitle
== wxEmptyString
)
579 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
593 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
595 wxString url
= wxFileSystem::FileNameToURL(filename
);
596 return LoadPage(url
);
600 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
602 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
605 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
612 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
613 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
614 m_OpenedAnchor
= anchor
;
620 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
625 tit
.Printf(m_TitleFormat
, title
.c_str());
626 m_RelatedFrame
->SetTitle(tit
);
628 m_OpenedPageTitle
= title
;
635 void wxHtmlWindow::CreateLayout()
637 int ClientWidth
, ClientHeight
;
641 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
643 SetScrollbars(1, 1, 0, 0); // always off
644 GetClientSize(&ClientWidth
, &ClientHeight
);
645 m_Cell
->Layout(ClientWidth
);
649 GetClientSize(&ClientWidth
, &ClientHeight
);
650 m_Cell
->Layout(ClientWidth
);
651 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
654 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
655 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
656 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
657 /*cheat: top-level frag is always container*/);
659 else /* we fit into window, no need for scrollbars */
661 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
662 GetClientSize(&ClientWidth
, &ClientHeight
);
663 m_Cell
->Layout(ClientWidth
); // ...and relayout
670 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
675 wxString p_fff
, p_ffn
;
677 if (path
!= wxEmptyString
)
679 oldpath
= cfg
->GetPath();
683 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
684 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
685 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
686 for (int i
= 0; i
< 7; i
++)
688 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
689 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
691 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
693 if (path
!= wxEmptyString
)
694 cfg
->SetPath(oldpath
);
699 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
704 if (path
!= wxEmptyString
)
706 oldpath
= cfg
->GetPath();
710 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
711 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
712 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
713 for (int i
= 0; i
< 7; i
++)
715 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
716 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
719 if (path
!= wxEmptyString
)
720 cfg
->SetPath(oldpath
);
725 bool wxHtmlWindow::HistoryBack()
729 if (m_HistoryPos
< 1) return false;
731 // store scroll position into history item:
733 GetViewStart(&x
, &y
);
734 (*m_History
)[m_HistoryPos
].SetPos(y
);
736 // go to previous position:
739 l
= (*m_History
)[m_HistoryPos
].GetPage();
740 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
743 if (a
== wxEmptyString
) LoadPage(l
);
744 else LoadPage(l
+ wxT("#") + a
);
747 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
752 bool wxHtmlWindow::HistoryCanBack()
754 if (m_HistoryPos
< 1) return false;
759 bool wxHtmlWindow::HistoryForward()
763 if (m_HistoryPos
== -1) return false;
764 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
766 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
769 l
= (*m_History
)[m_HistoryPos
].GetPage();
770 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
773 if (a
== wxEmptyString
) LoadPage(l
);
774 else LoadPage(l
+ wxT("#") + a
);
777 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
782 bool wxHtmlWindow::HistoryCanForward()
784 if (m_HistoryPos
== -1) return false;
785 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return false;
790 void wxHtmlWindow::HistoryClear()
796 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
800 m_Processors
= new wxHtmlProcessorList
;
802 wxHtmlProcessorList::compatibility_iterator node
;
804 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
806 if (processor
->GetPriority() > node
->GetData()->GetPriority())
808 m_Processors
->Insert(node
, processor
);
812 m_Processors
->Append(processor
);
815 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
817 if (!m_GlobalProcessors
)
819 m_GlobalProcessors
= new wxHtmlProcessorList
;
821 wxHtmlProcessorList::compatibility_iterator node
;
823 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
825 if (processor
->GetPriority() > node
->GetData()->GetPriority())
827 m_GlobalProcessors
->Insert(node
, processor
);
831 m_GlobalProcessors
->Append(processor
);
836 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
838 m_Filters
.Append(filter
);
842 bool wxHtmlWindow::IsSelectionEnabled() const
845 return !(m_Style
& wxHW_NO_SELECTION
);
853 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
856 return wxEmptyString
;
860 const wxHtmlCell
*end
= sel
->GetToCell();
862 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
865 text
<< i
->ConvertToText(sel
);
868 const wxHtmlCell
*prev
= *i
;
871 if ( prev
->GetParent() != i
->GetParent() )
873 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
880 wxString
wxHtmlWindow::ToText()
885 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
886 return DoSelectionToText(&sel
);
889 return wxEmptyString
;
892 #endif // wxUSE_CLIPBOARD
894 bool wxHtmlWindow::CopySelection(ClipboardType t
)
899 #if defined(__UNIX__) && !defined(__WXMAC__)
900 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
902 // Primary selection exists only under X11, so don't do anything under
903 // the other platforms when we try to access it
905 // TODO: this should be abstracted at wxClipboard level!
908 #endif // __UNIX__/!__UNIX__
910 if ( wxTheClipboard
->Open() )
912 const wxString
txt(SelectionToText());
913 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
914 wxTheClipboard
->Close();
915 wxLogTrace(_T("wxhtmlselection"),
916 _("Copied to clipboard:\"%s\""), txt
.c_str());
923 #endif // wxUSE_CLIPBOARD
929 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
931 const wxMouseEvent
*e
= link
.GetEvent();
932 if (e
== NULL
|| e
->LeftUp())
933 LoadPage(link
.GetHref());
936 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& event
)
940 // don't even skip the event, if we don't have a bg bitmap we're going
941 // to overwrite background in OnPaint() below anyhow, so letting the
942 // default handling take place would only result in flicker, just set a
943 // flag to erase the background below
944 m_eraseBgInOnPaint
= true;
948 wxDC
& dc
= *event
.GetDC();
950 // if the image is not fully opaque, we have to erase the background before
951 // drawing it, however avoid doing it for opaque images as this would just
952 // result in extra flicker without any other effect as background is
953 // completely covered anyhow
954 if ( m_bmpBg
.GetMask() )
956 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
960 const wxSize
sizeWin(GetClientSize());
961 const wxSize
sizeBmp(m_bmpBg
.GetWidth(), m_bmpBg
.GetHeight());
962 for ( wxCoord x
= 0; x
< sizeWin
.x
; x
+= sizeBmp
.x
)
964 for ( wxCoord y
= 0; y
< sizeWin
.y
; y
+= sizeBmp
.y
)
966 dc
.DrawBitmap(m_bmpBg
, x
, y
, true /* use mask */);
971 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
975 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
)
979 GetViewStart(&x
, &y
);
980 wxRect rect
= GetUpdateRegion().GetBox();
981 wxSize sz
= GetSize();
985 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
986 dcm
.SelectObject(*m_backBuffer
);
988 if ( m_eraseBgInOnPaint
)
990 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
993 m_eraseBgInOnPaint
= false;
995 else // someone has already erased the background, keep it
997 // preserve the existing background, otherwise we'd erase anything the
998 // user code had drawn in its EVT_ERASE_BACKGROUND handler when we do
999 // the Blit back below
1000 dcm
.Blit(0, rect
.GetTop(),
1001 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1007 dcm
.SetMapMode(wxMM_TEXT
);
1008 dcm
.SetBackgroundMode(wxTRANSPARENT
);
1010 wxHtmlRenderingInfo rinfo
;
1011 wxDefaultHtmlRenderingStyle rstyle
;
1012 rinfo
.SetSelection(m_selection
);
1013 rinfo
.SetStyle(&rstyle
);
1014 m_Cell
->Draw(dcm
, 0, 0,
1015 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
1016 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
1019 //#define DEBUG_HTML_SELECTION
1020 #ifdef DEBUG_HTML_SELECTION
1023 wxGetMousePosition(&xc
, &yc
);
1024 ScreenToClient(&xc
, &yc
);
1025 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1026 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
1027 wxHtmlCell
*before
=
1028 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
1030 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
1032 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
1033 dcm
.SetPen(*wxBLACK_PEN
);
1035 dcm
.DrawRectangle(at
->GetAbsPos(),
1036 wxSize(at
->GetWidth(),at
->GetHeight()));
1037 dcm
.SetPen(*wxGREEN_PEN
);
1039 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
1040 before
->GetWidth()-2,before
->GetHeight()-2);
1041 dcm
.SetPen(*wxRED_PEN
);
1043 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
1044 after
->GetWidth()-4,after
->GetHeight()-4);
1048 dcm
.SetDeviceOrigin(0,0);
1049 dc
.Blit(0, rect
.GetTop(),
1050 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
1058 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
1060 wxDELETE(m_backBuffer
);
1062 wxScrolledWindow::OnSize(event
);
1065 // Recompute selection if necessary:
1068 m_selection
->Set(m_selection
->GetFromCell(),
1069 m_selection
->GetToCell());
1070 m_selection
->ClearPrivPos();
1077 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
1079 wxHtmlWindowMouseHelper::HandleMouseMoved();
1082 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
1085 if ( event
.LeftDown() && IsSelectionEnabled() )
1087 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
1088 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
1090 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
1092 (void) CopySelection();
1096 m_makingSelection
= true;
1100 wxDELETE(m_selection
);
1103 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
1104 m_tmpSelFromCell
= NULL
;
1111 #endif // wxUSE_CLIPBOARD
1114 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
1117 if ( m_makingSelection
)
1120 m_makingSelection
= false;
1122 // did the user move the mouse far enough from starting point?
1123 if ( CopySelection(Primary
) )
1125 // we don't want mouse up event that ended selecting to be
1126 // handled as mouse click and e.g. follow hyperlink:
1130 #endif // wxUSE_CLIPBOARD
1134 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1135 wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell
, pos
, event
);
1140 void wxHtmlWindow::OnInternalIdle()
1142 wxWindow::OnInternalIdle();
1144 if (m_Cell
!= NULL
&& DidMouseMove())
1146 #ifdef DEBUG_HTML_SELECTION
1150 wxGetMousePosition(&xc
, &yc
);
1151 ScreenToClient(&xc
, &yc
);
1152 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1154 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1156 // handle selection update:
1157 if ( m_makingSelection
)
1159 if ( !m_tmpSelFromCell
)
1160 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1161 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1163 // NB: a trick - we adjust selFromPos to be upper left or bottom
1164 // right corner of the first cell of the selection depending
1165 // on whether the mouse is moving to the right or to the left.
1166 // This gives us more "natural" behaviour when selecting
1167 // a line (specifically, first cell of the next line is not
1168 // included if you drag selection from left to right over
1171 if ( !m_tmpSelFromCell
)
1173 dirFromPos
= m_tmpSelFromPos
;
1177 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1178 if ( x
< m_tmpSelFromPos
.x
)
1180 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1181 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1184 bool goingDown
= dirFromPos
.y
< y
||
1185 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1187 // determine selection span:
1188 if ( /*still*/ !m_tmpSelFromCell
)
1192 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1193 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1194 wxHTML_FIND_NEAREST_AFTER
);
1195 if (!m_tmpSelFromCell
)
1196 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1200 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1201 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1202 wxHTML_FIND_NEAREST_BEFORE
);
1203 if (!m_tmpSelFromCell
)
1204 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1208 wxHtmlCell
*selcell
= cell
;
1213 selcell
= m_Cell
->FindCellByPos(x
, y
,
1214 wxHTML_FIND_NEAREST_BEFORE
);
1216 selcell
= m_Cell
->GetLastTerminal();
1220 selcell
= m_Cell
->FindCellByPos(x
, y
,
1221 wxHTML_FIND_NEAREST_AFTER
);
1223 selcell
= m_Cell
->GetFirstTerminal();
1227 // NB: it may *rarely* happen that the code above didn't find one
1228 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1230 if ( selcell
&& m_tmpSelFromCell
)
1234 // start selecting only if mouse movement was big enough
1235 // (otherwise it was meant as mouse click, not selection):
1236 const int PRECISION
= 2;
1237 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1238 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1240 m_selection
= new wxHtmlSelection();
1245 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1247 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1248 wxPoint(x
,y
), selcell
); }
1251 m_selection
->Set(wxPoint(x
,y
), selcell
,
1252 m_tmpSelFromPos
, m_tmpSelFromCell
);
1254 m_selection
->ClearPrivPos();
1260 // handle cursor and status bar text changes:
1262 // NB: because we're passing in 'cell' and not 'm_Cell' (so that the
1263 // leaf cell lookup isn't done twice), we need to adjust the
1264 // position for the new root:
1265 wxPoint
posInCell(x
, y
);
1267 posInCell
-= cell
->GetAbsPos();
1268 wxHtmlWindowMouseHelper::HandleIdle(cell
, posInCell
);
1273 void wxHtmlWindow::StopAutoScrolling()
1275 if ( m_timerAutoScroll
)
1277 wxDELETE(m_timerAutoScroll
);
1281 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1283 StopAutoScrolling();
1287 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1289 // don't prevent the usual processing of the event from taking place
1292 // when a captured mouse leave a scrolled window we start generate
1293 // scrolling events to allow, for example, extending selection beyond the
1294 // visible area in some controls
1295 if ( wxWindow::GetCapture() == this )
1297 // where is the mouse leaving?
1299 wxPoint pt
= event
.GetPosition();
1302 orient
= wxHORIZONTAL
;
1305 else if ( pt
.y
< 0 )
1307 orient
= wxVERTICAL
;
1310 else // we're lower or to the right of the window
1312 wxSize size
= GetClientSize();
1313 if ( pt
.x
> size
.x
)
1315 orient
= wxHORIZONTAL
;
1316 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1318 else if ( pt
.y
> size
.y
)
1320 orient
= wxVERTICAL
;
1321 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1323 else // this should be impossible
1325 // but seems to happen sometimes under wxMSW - maybe it's a bug
1326 // there but for now just ignore it
1328 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1334 // only start the auto scroll timer if the window can be scrolled in
1336 if ( !HasScrollbar(orient
) )
1339 delete m_timerAutoScroll
;
1340 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1343 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1344 : wxEVT_SCROLLWIN_LINEDOWN
,
1348 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1352 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1354 if ( IsSelectionEnabled() && event
.GetKeyCode() == 'C' && event
.CmdDown() )
1356 (void) CopySelection();
1360 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1362 (void) CopySelection();
1365 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1367 // select word under cursor:
1368 if ( IsSelectionEnabled() )
1370 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1372 (void) CopySelection(Primary
);
1374 m_lastDoubleClick
= wxGetLocalTimeMillis();
1380 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1384 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1388 m_selection
= new wxHtmlSelection();
1389 m_selection
->Set(cell
, cell
);
1390 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1391 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1396 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1400 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1403 // We use following heuristic to find a "line": let the line be all
1404 // cells in same container as the cell under mouse cursor that are
1405 // neither completely above nor completely bellow the clicked cell
1406 // (i.e. are likely to be words positioned on same line of text).
1408 int y1
= cell
->GetAbsPos().y
;
1409 int y2
= y1
+ cell
->GetHeight();
1411 const wxHtmlCell
*c
;
1412 const wxHtmlCell
*before
= NULL
;
1413 const wxHtmlCell
*after
= NULL
;
1415 // find last cell of line:
1416 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1418 y
= c
->GetAbsPos().y
;
1419 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1427 // find first cell of line:
1428 for ( c
= cell
->GetParent()->GetFirstChild();
1429 c
&& c
!= cell
; c
= c
->GetNext())
1431 y
= c
->GetAbsPos().y
;
1432 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1444 m_selection
= new wxHtmlSelection();
1445 m_selection
->Set(before
, after
);
1452 void wxHtmlWindow::SelectAll()
1457 m_selection
= new wxHtmlSelection();
1458 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1463 #endif // wxUSE_CLIPBOARD
1467 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1469 #if wxUSE_EXTENDED_RTTI
1470 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1472 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1475 style , wxHW_SCROLLBAR_AUTO
1476 borders , (dimension)
1480 wxEND_PROPERTIES_TABLE()
1482 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1483 wxEND_HANDLERS_TABLE()
1485 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1487 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1490 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1491 EVT_SIZE(wxHtmlWindow::OnSize
)
1492 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1493 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1494 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1495 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1496 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1497 EVT_PAINT(wxHtmlWindow::OnPaint
)
1499 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1500 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1501 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1502 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1503 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1504 #endif // wxUSE_CLIPBOARD
1507 //-----------------------------------------------------------------------------
1508 // wxHtmlWindowInterface implementation in wxHtmlWindow
1509 //-----------------------------------------------------------------------------
1511 void wxHtmlWindow::SetHTMLWindowTitle(const wxString
& title
)
1516 void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo
& link
)
1518 OnLinkClicked(link
);
1521 wxHtmlOpeningStatus
wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type
,
1522 const wxString
& url
,
1523 wxString
*redirect
) const
1525 return OnOpeningURL(type
, url
, redirect
);
1528 wxPoint
wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell
*WXUNUSED(cell
),
1529 const wxPoint
& pos
) const
1531 return CalcScrolledPosition(pos
);
1534 wxWindow
* wxHtmlWindow::GetHTMLWindow()
1539 wxColour
wxHtmlWindow::GetHTMLBackgroundColour() const
1541 return GetBackgroundColour();
1544 void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour
& clr
)
1546 SetBackgroundColour(clr
);
1549 void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap
& bmpBg
)
1551 SetBackgroundImage(bmpBg
);
1554 void wxHtmlWindow::SetHTMLStatusText(const wxString
& text
)
1557 if (m_RelatedStatusBar
!= -1)
1558 m_RelatedFrame
->SetStatusText(text
, m_RelatedStatusBar
);
1559 #endif // wxUSE_STATUSBAR
1563 wxCursor
wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type
)
1567 case HTMLCursor_Link
:
1568 if ( !ms_cursorLink
)
1569 ms_cursorLink
= new wxCursor(wxCURSOR_HAND
);
1570 return *ms_cursorLink
;
1572 case HTMLCursor_Text
:
1573 if ( !ms_cursorText
)
1574 ms_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
1575 return *ms_cursorText
;
1577 case HTMLCursor_Default
:
1579 return *wxSTANDARD_CURSOR
;
1583 wxCursor
wxHtmlWindow::GetHTMLCursor(HTMLCursor type
) const
1585 return GetDefaultHTMLCursor(type
);
1589 //-----------------------------------------------------------------------------
1591 //-----------------------------------------------------------------------------
1593 // A module to allow initialization/cleanup
1594 // without calling these functions from app.cpp or from
1595 // the user's application.
1597 class wxHtmlWinModule
: public wxModule
1599 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1601 wxHtmlWinModule() : wxModule() {}
1602 bool OnInit() { return true; }
1603 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1606 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1609 // This hack forces the linker to always link in m_* files
1610 // (wxHTML doesn't work without handlers from these files)
1611 #include "wx/html/forcelnk.h"
1612 FORCE_WXHTML_MODULES()
1614 #endif // wxUSE_HTML