1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "htmlwin.h"
13 #pragma implementation "htmlproc.h"
16 #include "wx/wxprec.h"
19 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/dcclient.h"
32 #include "wx/html/htmlwin.h"
33 #include "wx/html/htmlproc.h"
35 #include "wx/clipbrd.h"
36 #include "wx/dataobj.h"
38 #include "wx/dcmemory.h"
40 #include "wx/arrimpl.cpp"
41 #include "wx/listimpl.cpp"
46 // ----------------------------------------------------------------------------
47 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
48 // events when a captured mouse is held outside the window
49 // ----------------------------------------------------------------------------
51 class wxHtmlWinAutoScrollTimer
: public wxTimer
54 wxHtmlWinAutoScrollTimer(wxScrolledWindow
*win
,
55 wxEventType eventTypeToSend
,
59 m_eventType
= eventTypeToSend
;
64 virtual void Notify();
67 wxScrolledWindow
*m_win
;
68 wxEventType m_eventType
;
72 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer
)
75 void wxHtmlWinAutoScrollTimer::Notify()
77 // only do all this as long as the window is capturing the mouse
78 if ( wxWindow::GetCapture() != m_win
)
82 else // we still capture the mouse, continue generating events
84 // first scroll the window if we are allowed to do it
85 wxScrollWinEvent
event1(m_eventType
, m_pos
, m_orient
);
86 event1
.SetEventObject(m_win
);
87 if ( m_win
->GetEventHandler()->ProcessEvent(event1
) )
89 // and then send a pseudo mouse-move event to refresh the selection
90 wxMouseEvent
event2(wxEVT_MOTION
);
91 wxGetMousePosition(&event2
.m_x
, &event2
.m_y
);
93 // the mouse event coordinates should be client, not screen as
94 // returned by wxGetMousePosition
95 wxWindow
*parentTop
= m_win
;
96 while ( parentTop
->GetParent() )
97 parentTop
= parentTop
->GetParent();
98 wxPoint ptOrig
= parentTop
->GetPosition();
99 event2
.m_x
-= ptOrig
.x
;
100 event2
.m_y
-= ptOrig
.y
;
102 event2
.SetEventObject(m_win
);
104 // FIXME: we don't fill in the other members - ok?
105 m_win
->GetEventHandler()->ProcessEvent(event2
);
107 else // can't scroll further, stop
114 #endif // wxUSE_CLIPBOARD
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 // item of history list
123 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
126 wxHtmlHistoryItem(const wxString
& p
, const wxString
& a
) {m_Page
= p
, m_Anchor
= a
, m_Pos
= 0;}
127 int GetPos() const {return m_Pos
;}
128 void SetPos(int p
) {m_Pos
= p
;}
129 const wxString
& GetPage() const {return m_Page
;}
130 const wxString
& GetAnchor() const {return m_Anchor
;}
139 //-----------------------------------------------------------------------------
140 // our private arrays:
141 //-----------------------------------------------------------------------------
143 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem
, wxHtmlHistoryArray
);
144 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray
);
146 WX_DECLARE_LIST(wxHtmlProcessor
, wxHtmlProcessorList
);
147 WX_DEFINE_LIST(wxHtmlProcessorList
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
154 void wxHtmlWindow::Init()
156 m_tmpMouseMoved
= FALSE
;
157 m_tmpLastLink
= NULL
;
158 m_tmpLastCell
= NULL
;
159 m_tmpCanDrawLocks
= 0;
160 m_FS
= new wxFileSystem();
162 m_RelatedStatusBar
= -1;
163 #endif // wxUSE_STATUSBAR
164 m_RelatedFrame
= NULL
;
165 m_TitleFormat
= wxT("%s");
166 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
168 m_Parser
= new wxHtmlWinParser(this);
169 m_Parser
->SetFS(m_FS
);
172 m_History
= new wxHtmlHistoryArray
;
177 m_makingSelection
= false;
179 m_timerAutoScroll
= NULL
;
180 m_lastDoubleClick
= 0;
181 #endif // wxUSE_CLIPBOARD
185 bool wxHtmlWindow::Create(wxWindow
*parent
, wxWindowID id
,
186 const wxPoint
& pos
, const wxSize
& size
,
187 long style
, const wxString
& name
)
189 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
190 style
| wxVSCROLL
| wxHSCROLL
,
195 SetPage(wxT("<html><body></body></html>"));
200 wxHtmlWindow::~wxHtmlWindow()
204 #endif // wxUSE_CLIPBOARD
213 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_Processors
);
225 void wxHtmlWindow::SetRelatedFrame(wxFrame
* frame
, const wxString
& format
)
227 m_RelatedFrame
= frame
;
228 m_TitleFormat
= format
;
234 void wxHtmlWindow::SetRelatedStatusBar(int bar
)
236 m_RelatedStatusBar
= bar
;
238 #endif // wxUSE_STATUSBAR
242 void wxHtmlWindow::SetFonts(wxString normal_face
, wxString fixed_face
, const int *sizes
)
244 wxString op
= m_OpenedPage
;
246 m_Parser
->SetFonts(normal_face
, fixed_face
, sizes
);
247 // fonts changed => contents invalid, so reload the page:
248 SetPage(wxT("<html><body></body></html>"));
249 if (!op
.IsEmpty()) LoadPage(op
);
254 bool wxHtmlWindow::SetPage(const wxString
& source
)
256 wxString
newsrc(source
);
258 wxDELETE(m_selection
);
260 // pass HTML through registered processors:
261 if (m_Processors
|| m_GlobalProcessors
)
263 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
266 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
267 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
269 // VS: there are two lists, global and local, both of them sorted by
270 // priority. Since we have to go through _both_ lists with
271 // decreasing priority, we "merge-sort" the lists on-line by
272 // processing that one of the two heads that has higher priority
273 // in every iteration
274 while (nodeL
|| nodeG
)
276 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
277 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
280 if (nodeL
->GetData()->IsEnabled())
281 newsrc
= nodeL
->GetData()->Process(newsrc
);
282 nodeL
= nodeL
->GetNext();
286 if (nodeG
->GetData()->IsEnabled())
287 newsrc
= nodeG
->GetData()->Process(newsrc
);
288 nodeG
= nodeG
->GetNext();
293 // ...and run the parser on it:
294 wxClientDC
*dc
= new wxClientDC(this);
295 dc
->SetMapMode(wxMM_TEXT
);
296 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
297 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
304 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
306 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
307 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
309 if (m_tmpCanDrawLocks
== 0)
314 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
316 return SetPage(*(GetParser()->GetSource()) + source
);
319 bool wxHtmlWindow::LoadPage(const wxString
& location
)
321 wxBusyCursor busyCursor
;
325 bool needs_refresh
= FALSE
;
328 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
330 // store scroll position into history item:
332 GetViewStart(&x
, &y
);
333 (*m_History
)[m_HistoryPos
].SetPos(y
);
336 if (location
[0] == wxT('#'))
339 wxString anch
= location
.Mid(1) /*1 to end*/;
341 rt_val
= ScrollToAnchor(anch
);
344 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
346 wxString anch
= location
.AfterFirst(wxT('#'));
348 rt_val
= ScrollToAnchor(anch
);
351 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
352 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
354 wxString anch
= location
.AfterFirst(wxT('#'));
356 rt_val
= ScrollToAnchor(anch
);
362 needs_refresh
= true;
365 if (m_RelatedStatusBar
!= -1)
367 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
370 #endif // wxUSE_STATUSBAR
372 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
374 // try to interpret 'location' as filename instead of URL:
377 wxFileName
fn(location
);
378 wxString location2
= wxFileSystem::FileNameToURL(fn
);
379 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
384 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
391 wxList::compatibility_iterator node
;
392 wxString src
= wxEmptyString
;
395 if (m_RelatedStatusBar
!= -1)
397 wxString msg
= _("Loading : ") + location
;
398 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
401 #endif // wxUSE_STATUSBAR
403 node
= m_Filters
.GetFirst();
406 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
409 src
= h
->ReadFile(*f
);
412 node
= node
->GetNext();
414 if (src
== wxEmptyString
)
416 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
417 src
= m_DefaultFilter
->ReadFile(*f
);
420 m_FS
->ChangePathTo(f
->GetLocation());
421 rt_val
= SetPage(src
);
422 m_OpenedPage
= f
->GetLocation();
423 if (f
->GetAnchor() != wxEmptyString
)
425 ScrollToAnchor(f
->GetAnchor());
431 if (m_RelatedStatusBar
!= -1)
432 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
433 #endif // wxUSE_STATUSBAR
437 if (m_HistoryOn
) // add this page to history there:
439 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
441 if (m_HistoryPos
< 0 ||
442 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
443 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
446 for (int i
= 0; i
< c
; i
++)
447 m_History
->RemoveAt(m_HistoryPos
);
448 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
452 if (m_OpenedPageTitle
== wxEmptyString
)
453 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
467 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
469 wxString url
= wxFileSystem::FileNameToURL(filename
);
470 return LoadPage(url
);
474 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
476 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
479 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
486 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
487 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
488 m_OpenedAnchor
= anchor
;
494 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
499 tit
.Printf(m_TitleFormat
, title
.c_str());
500 m_RelatedFrame
->SetTitle(tit
);
502 m_OpenedPageTitle
= title
;
509 void wxHtmlWindow::CreateLayout()
511 int ClientWidth
, ClientHeight
;
515 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
517 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
518 GetClientSize(&ClientWidth
, &ClientHeight
);
519 m_Cell
->Layout(ClientWidth
);
523 GetClientSize(&ClientWidth
, &ClientHeight
);
524 m_Cell
->Layout(ClientWidth
);
525 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
528 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
529 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
530 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
531 /*cheat: top-level frag is always container*/);
533 else /* we fit into window, no need for scrollbars */
535 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
536 GetClientSize(&ClientWidth
, &ClientHeight
);
537 m_Cell
->Layout(ClientWidth
); // ...and relayout
544 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
549 wxString p_fff
, p_ffn
;
551 if (path
!= wxEmptyString
)
553 oldpath
= cfg
->GetPath();
557 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
558 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
559 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
560 for (int i
= 0; i
< 7; i
++)
562 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
563 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
565 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
567 if (path
!= wxEmptyString
)
568 cfg
->SetPath(oldpath
);
573 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
578 if (path
!= wxEmptyString
)
580 oldpath
= cfg
->GetPath();
584 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
585 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
586 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
587 for (int i
= 0; i
< 7; i
++)
589 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
590 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
593 if (path
!= wxEmptyString
)
594 cfg
->SetPath(oldpath
);
599 bool wxHtmlWindow::HistoryBack()
603 if (m_HistoryPos
< 1) return FALSE
;
605 // store scroll position into history item:
607 GetViewStart(&x
, &y
);
608 (*m_History
)[m_HistoryPos
].SetPos(y
);
610 // go to previous position:
613 l
= (*m_History
)[m_HistoryPos
].GetPage();
614 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
617 if (a
== wxEmptyString
) LoadPage(l
);
618 else LoadPage(l
+ wxT("#") + a
);
621 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
626 bool wxHtmlWindow::HistoryCanBack()
628 if (m_HistoryPos
< 1) return FALSE
;
633 bool wxHtmlWindow::HistoryForward()
637 if (m_HistoryPos
== -1) return FALSE
;
638 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
640 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
643 l
= (*m_History
)[m_HistoryPos
].GetPage();
644 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
647 if (a
== wxEmptyString
) LoadPage(l
);
648 else LoadPage(l
+ wxT("#") + a
);
651 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
656 bool wxHtmlWindow::HistoryCanForward()
658 if (m_HistoryPos
== -1) return FALSE
;
659 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
664 void wxHtmlWindow::HistoryClear()
670 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
674 m_Processors
= new wxHtmlProcessorList
;
676 wxHtmlProcessorList::compatibility_iterator node
;
678 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
680 if (processor
->GetPriority() > node
->GetData()->GetPriority())
682 m_Processors
->Insert(node
, processor
);
686 m_Processors
->Append(processor
);
689 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
691 if (!m_GlobalProcessors
)
693 m_GlobalProcessors
= new wxHtmlProcessorList
;
695 wxHtmlProcessorList::compatibility_iterator node
;
697 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
699 if (processor
->GetPriority() > node
->GetData()->GetPriority())
701 m_GlobalProcessors
->Insert(node
, processor
);
705 m_GlobalProcessors
->Append(processor
);
710 wxList
wxHtmlWindow::m_Filters
;
711 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
712 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
714 void wxHtmlWindow::CleanUpStatics()
716 wxDELETE(m_DefaultFilter
);
717 WX_CLEAR_LIST(wxList
, m_Filters
);
718 if (m_GlobalProcessors
)
719 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
720 wxDELETE(m_GlobalProcessors
);
725 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
727 m_Filters
.Append(filter
);
731 bool wxHtmlWindow::IsSelectionEnabled() const
734 return !(m_Style
& wxHW_NO_SELECTION
);
742 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
745 return wxEmptyString
;
749 const wxHtmlCell
*end
= sel
->GetToCell();
751 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
754 text
<< i
->ConvertToText(sel
);
757 const wxHtmlCell
*prev
= *i
;
760 if ( prev
->GetParent() != i
->GetParent() )
762 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
769 wxString
wxHtmlWindow::ToText()
774 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
775 return DoSelectionToText(&sel
);
778 return wxEmptyString
;
781 #endif // wxUSE_CLIPBOARD
783 bool wxHtmlWindow::CopySelection(ClipboardType t
)
789 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
791 // Primary selection exists only under X11, so don't do anything under
792 // the other platforms when we try to access it
794 // TODO: this should be abstracted at wxClipboard level!
797 #endif // __UNIX__/!__UNIX__
799 if ( wxTheClipboard
->Open() )
801 const wxString
txt(SelectionToText());
802 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
803 wxTheClipboard
->Close();
804 wxLogTrace(_T("wxhtmlselection"),
805 _("Copied to clipboard:\"%s\""), txt
.c_str());
810 #endif // wxUSE_CLIPBOARD
816 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
818 const wxMouseEvent
*e
= link
.GetEvent();
819 if (e
== NULL
|| e
->LeftUp())
820 LoadPage(link
.GetHref());
823 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
824 wxCoord x
, wxCoord y
,
825 const wxMouseEvent
& event
)
827 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
829 cell
->OnMouseClick(this, x
, y
, event
);
832 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
833 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
838 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
842 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
846 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
849 GetViewStart(&x
, &y
);
850 wxRect rect
= GetUpdateRegion().GetBox();
851 wxSize sz
= GetSize();
855 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
856 dcm
.SelectObject(*m_backBuffer
);
857 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
860 dcm
.SetMapMode(wxMM_TEXT
);
861 dcm
.SetBackgroundMode(wxTRANSPARENT
);
863 wxHtmlRenderingInfo rinfo
;
864 wxDefaultHtmlRenderingStyle rstyle
;
865 rinfo
.SetSelection(m_selection
);
866 rinfo
.SetStyle(&rstyle
);
867 m_Cell
->Draw(dcm
, 0, 0,
868 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
869 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
872 //#define DEBUG_HTML_SELECTION
873 #ifdef DEBUG_HTML_SELECTION
876 wxGetMousePosition(&xc
, &yc
);
877 ScreenToClient(&xc
, &yc
);
878 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
879 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
881 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
883 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
885 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
886 dcm
.SetPen(*wxBLACK_PEN
);
888 dcm
.DrawRectangle(at
->GetAbsPos(),
889 wxSize(at
->GetWidth(),at
->GetHeight()));
890 dcm
.SetPen(*wxGREEN_PEN
);
892 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
893 before
->GetWidth()-2,before
->GetHeight()-2);
894 dcm
.SetPen(*wxRED_PEN
);
896 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
897 after
->GetWidth()-4,after
->GetHeight()-4);
901 dcm
.SetDeviceOrigin(0,0);
902 dc
.Blit(0, rect
.GetTop(),
903 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
911 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
913 wxDELETE(m_backBuffer
);
915 wxScrolledWindow::OnSize(event
);
918 // Recompute selection if necessary:
921 m_selection
->Set(m_selection
->GetFromCell(),
922 m_selection
->GetToCell());
923 m_selection
->ClearPrivPos();
930 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
932 m_tmpMouseMoved
= true;
935 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
938 if ( event
.LeftDown() && IsSelectionEnabled() )
940 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
941 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
943 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
945 (void) CopySelection();
949 m_makingSelection
= true;
953 wxDELETE(m_selection
);
956 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
957 m_tmpSelFromCell
= NULL
;
962 #endif // wxUSE_CLIPBOARD
965 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
968 if ( m_makingSelection
)
971 m_makingSelection
= false;
973 // did the user move the mouse far enough from starting point?
974 if ( CopySelection(Primary
) )
976 // we don't want mouse up event that ended selecting to be
977 // handled as mouse click and e.g. follow hyperlink:
981 #endif // wxUSE_CLIPBOARD
986 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
987 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
989 // check is needed because FindCellByPos returns terminal cell and
990 // containers may have empty borders -- in this case NULL will be
993 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
999 void wxHtmlWindow::OnInternalIdle()
1001 wxWindow::OnInternalIdle();
1003 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1005 #ifdef DEBUG_HTML_SELECTION
1009 wxGetMousePosition(&xc
, &yc
);
1010 ScreenToClient(&xc
, &yc
);
1011 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1013 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1015 // handle selection update:
1016 if ( m_makingSelection
)
1018 if ( !m_tmpSelFromCell
)
1019 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1020 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1022 // NB: a trick - we adjust selFromPos to be upper left or bottom
1023 // right corner of the first cell of the selection depending
1024 // on whether the mouse is moving to the right or to the left.
1025 // This gives us more "natural" behaviour when selecting
1026 // a line (specifically, first cell of the next line is not
1027 // included if you drag selection from left to right over
1030 if ( !m_tmpSelFromCell
)
1032 dirFromPos
= m_tmpSelFromPos
;
1036 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1037 if ( x
< m_tmpSelFromPos
.x
)
1039 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1040 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1043 bool goingDown
= dirFromPos
.y
< y
||
1044 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1046 // determine selection span:
1047 if ( /*still*/ !m_tmpSelFromCell
)
1051 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1052 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1053 wxHTML_FIND_NEAREST_AFTER
);
1054 if (!m_tmpSelFromCell
)
1055 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1059 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1060 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1061 wxHTML_FIND_NEAREST_BEFORE
);
1062 if (!m_tmpSelFromCell
)
1063 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1067 wxHtmlCell
*selcell
= cell
;
1072 selcell
= m_Cell
->FindCellByPos(x
, y
,
1073 wxHTML_FIND_NEAREST_BEFORE
);
1075 selcell
= m_Cell
->GetLastTerminal();
1079 selcell
= m_Cell
->FindCellByPos(x
, y
,
1080 wxHTML_FIND_NEAREST_AFTER
);
1082 selcell
= m_Cell
->GetFirstTerminal();
1086 // NB: it may *rarely* happen that the code above didn't find one
1087 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1089 if ( selcell
&& m_tmpSelFromCell
)
1093 // start selecting only if mouse movement was big enough
1094 // (otherwise it was meant as mouse click, not selection):
1095 const int PRECISION
= 2;
1096 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1097 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1099 m_selection
= new wxHtmlSelection();
1104 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1106 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1107 wxPoint(x
,y
), selcell
); }
1110 m_selection
->Set(wxPoint(x
,y
), selcell
,
1111 m_tmpSelFromPos
, m_tmpSelFromCell
);
1113 m_selection
->ClearPrivPos();
1119 // handle cursor and status bar text changes:
1120 if ( cell
!= m_tmpLastCell
)
1122 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1125 cur
= cell
->GetCursor();
1127 cur
= *wxSTANDARD_CURSOR
;
1130 if (lnk
!= m_tmpLastLink
)
1135 if (m_RelatedStatusBar
!= -1)
1136 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1137 m_RelatedStatusBar
);
1141 if (m_RelatedStatusBar
!= -1)
1142 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1143 m_RelatedStatusBar
);
1145 #endif // wxUSE_STATUSBAR
1146 m_tmpLastLink
= lnk
;
1149 m_tmpLastCell
= cell
;
1151 else // mouse moved but stayed in the same cell
1154 OnCellMouseHover(cell
, x
, y
);
1157 m_tmpMouseMoved
= FALSE
;
1162 void wxHtmlWindow::StopAutoScrolling()
1164 if ( m_timerAutoScroll
)
1166 wxDELETE(m_timerAutoScroll
);
1170 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1172 StopAutoScrolling();
1176 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1178 // don't prevent the usual processing of the event from taking place
1181 // when a captured mouse leave a scrolled window we start generate
1182 // scrolling events to allow, for example, extending selection beyond the
1183 // visible area in some controls
1184 if ( wxWindow::GetCapture() == this )
1186 // where is the mouse leaving?
1188 wxPoint pt
= event
.GetPosition();
1191 orient
= wxHORIZONTAL
;
1194 else if ( pt
.y
< 0 )
1196 orient
= wxVERTICAL
;
1199 else // we're lower or to the right of the window
1201 wxSize size
= GetClientSize();
1202 if ( pt
.x
> size
.x
)
1204 orient
= wxHORIZONTAL
;
1205 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1207 else if ( pt
.y
> size
.y
)
1209 orient
= wxVERTICAL
;
1210 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1212 else // this should be impossible
1214 // but seems to happen sometimes under wxMSW - maybe it's a bug
1215 // there but for now just ignore it
1217 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1223 // only start the auto scroll timer if the window can be scrolled in
1225 if ( !HasScrollbar(orient
) )
1228 delete m_timerAutoScroll
;
1229 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1232 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1233 : wxEVT_SCROLLWIN_LINEDOWN
,
1237 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1241 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1243 if ( IsSelectionEnabled() &&
1244 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1246 (void) CopySelection();
1250 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1252 (void) CopySelection();
1255 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1257 // select word under cursor:
1258 if ( IsSelectionEnabled() )
1260 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1262 (void) CopySelection(Primary
);
1264 m_lastDoubleClick
= wxGetLocalTimeMillis();
1270 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1274 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1278 m_selection
= new wxHtmlSelection();
1279 m_selection
->Set(cell
, cell
);
1280 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1281 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1286 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1290 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1293 // We use following heuristic to find a "line": let the line be all
1294 // cells in same container as the cell under mouse cursor that are
1295 // neither completely above nor completely bellow the clicked cell
1296 // (i.e. are likely to be words positioned on same line of text).
1298 int y1
= cell
->GetAbsPos().y
;
1299 int y2
= y1
+ cell
->GetHeight();
1301 const wxHtmlCell
*c
;
1302 const wxHtmlCell
*before
= NULL
;
1303 const wxHtmlCell
*after
= NULL
;
1305 // find last cell of line:
1306 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1308 y
= c
->GetAbsPos().y
;
1309 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1317 // find first cell of line:
1318 for ( c
= cell
->GetParent()->GetFirstChild();
1319 c
&& c
!= cell
; c
= c
->GetNext())
1321 y
= c
->GetAbsPos().y
;
1322 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1334 m_selection
= new wxHtmlSelection();
1335 m_selection
->Set(before
, after
);
1342 void wxHtmlWindow::SelectAll()
1347 m_selection
= new wxHtmlSelection();
1348 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1353 #endif // wxUSE_CLIPBOARD
1357 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1359 #if wxUSE_EXTENDED_RTTI
1360 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1362 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1365 style , wxHW_SCROLLBAR_AUTO
1366 borders , (dimension)
1370 wxEND_PROPERTIES_TABLE()
1372 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1373 wxEND_HANDLERS_TABLE()
1375 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1377 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1380 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1381 EVT_SIZE(wxHtmlWindow::OnSize
)
1382 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1383 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1384 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1385 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1386 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1387 EVT_PAINT(wxHtmlWindow::OnPaint
)
1389 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1390 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1391 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1392 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1393 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1394 #endif // wxUSE_CLIPBOARD
1401 // A module to allow initialization/cleanup
1402 // without calling these functions from app.cpp or from
1403 // the user's application.
1405 class wxHtmlWinModule
: public wxModule
1407 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1409 wxHtmlWinModule() : wxModule() {}
1410 bool OnInit() { return TRUE
; }
1411 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1414 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1417 // This hack forces the linker to always link in m_* files
1418 // (wxHTML doesn't work without handlers from these files)
1419 #include "wx/html/forcelnk.h"
1420 FORCE_WXHTML_MODULES()
1422 #endif // wxUSE_HTML