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"
39 #include "wx/settings.h"
41 #include "wx/arrimpl.cpp"
42 #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
);
252 void wxHtmlWindow::NormalizeFontSizes(int size
)
256 size
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
).GetPointSize();
258 f_sizes
[0] = int(size
* 0.6);
259 f_sizes
[1] = int(size
* 0.8);
261 f_sizes
[3] = int(size
* 1.2);
262 f_sizes
[4] = int(size
* 1.4);
263 f_sizes
[5] = int(size
* 1.6);
264 f_sizes
[6] = int(size
* 1.8);
266 SetFonts(wxEmptyString
, wxEmptyString
, f_sizes
);
270 bool wxHtmlWindow::SetPage(const wxString
& source
)
272 wxString
newsrc(source
);
274 wxDELETE(m_selection
);
276 // pass HTML through registered processors:
277 if (m_Processors
|| m_GlobalProcessors
)
279 wxHtmlProcessorList::compatibility_iterator nodeL
, nodeG
;
282 nodeL
= (m_Processors
) ? m_Processors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
283 nodeG
= (m_GlobalProcessors
) ? m_GlobalProcessors
->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
285 // VS: there are two lists, global and local, both of them sorted by
286 // priority. Since we have to go through _both_ lists with
287 // decreasing priority, we "merge-sort" the lists on-line by
288 // processing that one of the two heads that has higher priority
289 // in every iteration
290 while (nodeL
|| nodeG
)
292 prL
= (nodeL
) ? nodeL
->GetData()->GetPriority() : -1;
293 prG
= (nodeG
) ? nodeG
->GetData()->GetPriority() : -1;
296 if (nodeL
->GetData()->IsEnabled())
297 newsrc
= nodeL
->GetData()->Process(newsrc
);
298 nodeL
= nodeL
->GetNext();
302 if (nodeG
->GetData()->IsEnabled())
303 newsrc
= nodeG
->GetData()->Process(newsrc
);
304 nodeG
= nodeG
->GetNext();
309 // ...and run the parser on it:
310 wxClientDC
*dc
= new wxClientDC(this);
311 dc
->SetMapMode(wxMM_TEXT
);
312 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
313 m_OpenedPage
= m_OpenedAnchor
= m_OpenedPageTitle
= wxEmptyString
;
320 m_Cell
= (wxHtmlContainerCell
*) m_Parser
->Parse(newsrc
);
322 m_Cell
->SetIndent(m_Borders
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
323 m_Cell
->SetAlignHor(wxHTML_ALIGN_CENTER
);
325 if (m_tmpCanDrawLocks
== 0)
330 bool wxHtmlWindow::AppendToPage(const wxString
& source
)
332 return SetPage(*(GetParser()->GetSource()) + source
);
335 bool wxHtmlWindow::LoadPage(const wxString
& location
)
337 wxBusyCursor busyCursor
;
341 bool needs_refresh
= FALSE
;
344 if (m_HistoryOn
&& (m_HistoryPos
!= -1))
346 // store scroll position into history item:
348 GetViewStart(&x
, &y
);
349 (*m_History
)[m_HistoryPos
].SetPos(y
);
352 if (location
[0] == wxT('#'))
355 wxString anch
= location
.Mid(1) /*1 to end*/;
357 rt_val
= ScrollToAnchor(anch
);
360 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&& location
.BeforeFirst(wxT('#')) == m_OpenedPage
)
362 wxString anch
= location
.AfterFirst(wxT('#'));
364 rt_val
= ScrollToAnchor(anch
);
367 else if (location
.Find(wxT('#')) != wxNOT_FOUND
&&
368 (m_FS
->GetPath() + location
.BeforeFirst(wxT('#'))) == m_OpenedPage
)
370 wxString anch
= location
.AfterFirst(wxT('#'));
372 rt_val
= ScrollToAnchor(anch
);
378 needs_refresh
= true;
381 if (m_RelatedStatusBar
!= -1)
383 m_RelatedFrame
->SetStatusText(_("Connecting..."), m_RelatedStatusBar
);
386 #endif // wxUSE_STATUSBAR
388 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location
);
390 // try to interpret 'location' as filename instead of URL:
393 wxFileName
fn(location
);
394 wxString location2
= wxFileSystem::FileNameToURL(fn
);
395 f
= m_Parser
->OpenURL(wxHTML_URL_PAGE
, location2
);
400 wxLogError(_("Unable to open requested HTML document: %s"), location
.c_str());
407 wxList::compatibility_iterator node
;
408 wxString src
= wxEmptyString
;
411 if (m_RelatedStatusBar
!= -1)
413 wxString msg
= _("Loading : ") + location
;
414 m_RelatedFrame
->SetStatusText(msg
, m_RelatedStatusBar
);
417 #endif // wxUSE_STATUSBAR
419 node
= m_Filters
.GetFirst();
422 wxHtmlFilter
*h
= (wxHtmlFilter
*) node
->GetData();
425 src
= h
->ReadFile(*f
);
428 node
= node
->GetNext();
430 if (src
== wxEmptyString
)
432 if (m_DefaultFilter
== NULL
) m_DefaultFilter
= GetDefaultFilter();
433 src
= m_DefaultFilter
->ReadFile(*f
);
436 m_FS
->ChangePathTo(f
->GetLocation());
437 rt_val
= SetPage(src
);
438 m_OpenedPage
= f
->GetLocation();
439 if (f
->GetAnchor() != wxEmptyString
)
441 ScrollToAnchor(f
->GetAnchor());
447 if (m_RelatedStatusBar
!= -1)
448 m_RelatedFrame
->SetStatusText(_("Done"), m_RelatedStatusBar
);
449 #endif // wxUSE_STATUSBAR
453 if (m_HistoryOn
) // add this page to history there:
455 int c
= m_History
->GetCount() - (m_HistoryPos
+ 1);
457 if (m_HistoryPos
< 0 ||
458 (*m_History
)[m_HistoryPos
].GetPage() != m_OpenedPage
||
459 (*m_History
)[m_HistoryPos
].GetAnchor() != m_OpenedAnchor
)
462 for (int i
= 0; i
< c
; i
++)
463 m_History
->RemoveAt(m_HistoryPos
);
464 m_History
->Add(new wxHtmlHistoryItem(m_OpenedPage
, m_OpenedAnchor
));
468 if (m_OpenedPageTitle
== wxEmptyString
)
469 OnSetTitle(wxFileNameFromPath(m_OpenedPage
));
483 bool wxHtmlWindow::LoadFile(const wxFileName
& filename
)
485 wxString url
= wxFileSystem::FileNameToURL(filename
);
486 return LoadPage(url
);
490 bool wxHtmlWindow::ScrollToAnchor(const wxString
& anchor
)
492 const wxHtmlCell
*c
= m_Cell
->Find(wxHTML_COND_ISANCHOR
, &anchor
);
495 wxLogWarning(_("HTML anchor %s does not exist."), anchor
.c_str());
502 for (y
= 0; c
!= NULL
; c
= c
->GetParent()) y
+= c
->GetPosY();
503 Scroll(-1, y
/ wxHTML_SCROLL_STEP
);
504 m_OpenedAnchor
= anchor
;
510 void wxHtmlWindow::OnSetTitle(const wxString
& title
)
515 tit
.Printf(m_TitleFormat
, title
.c_str());
516 m_RelatedFrame
->SetTitle(tit
);
518 m_OpenedPageTitle
= title
;
525 void wxHtmlWindow::CreateLayout()
527 int ClientWidth
, ClientHeight
;
531 if (m_Style
& wxHW_SCROLLBAR_NEVER
)
533 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // always off
534 GetClientSize(&ClientWidth
, &ClientHeight
);
535 m_Cell
->Layout(ClientWidth
);
539 GetClientSize(&ClientWidth
, &ClientHeight
);
540 m_Cell
->Layout(ClientWidth
);
541 if (ClientHeight
< m_Cell
->GetHeight() + GetCharHeight())
544 wxHTML_SCROLL_STEP
, wxHTML_SCROLL_STEP
,
545 m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
,
546 (m_Cell
->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
547 /*cheat: top-level frag is always container*/);
549 else /* we fit into window, no need for scrollbars */
551 SetScrollbars(wxHTML_SCROLL_STEP
, 1, m_Cell
->GetWidth() / wxHTML_SCROLL_STEP
, 0); // disable...
552 GetClientSize(&ClientWidth
, &ClientHeight
);
553 m_Cell
->Layout(ClientWidth
); // ...and relayout
560 void wxHtmlWindow::ReadCustomization(wxConfigBase
*cfg
, wxString path
)
565 wxString p_fff
, p_ffn
;
567 if (path
!= wxEmptyString
)
569 oldpath
= cfg
->GetPath();
573 m_Borders
= cfg
->Read(wxT("wxHtmlWindow/Borders"), m_Borders
);
574 p_fff
= cfg
->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
575 p_ffn
= cfg
->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
576 for (int i
= 0; i
< 7; i
++)
578 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
579 p_fontsizes
[i
] = cfg
->Read(tmp
, m_Parser
->m_FontsSizes
[i
]);
581 SetFonts(p_ffn
, p_fff
, p_fontsizes
);
583 if (path
!= wxEmptyString
)
584 cfg
->SetPath(oldpath
);
589 void wxHtmlWindow::WriteCustomization(wxConfigBase
*cfg
, wxString path
)
594 if (path
!= wxEmptyString
)
596 oldpath
= cfg
->GetPath();
600 cfg
->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders
);
601 cfg
->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser
->m_FontFaceFixed
);
602 cfg
->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser
->m_FontFaceNormal
);
603 for (int i
= 0; i
< 7; i
++)
605 tmp
.Printf(wxT("wxHtmlWindow/FontsSize%i"), i
);
606 cfg
->Write(tmp
, (long) m_Parser
->m_FontsSizes
[i
]);
609 if (path
!= wxEmptyString
)
610 cfg
->SetPath(oldpath
);
615 bool wxHtmlWindow::HistoryBack()
619 if (m_HistoryPos
< 1) return FALSE
;
621 // store scroll position into history item:
623 GetViewStart(&x
, &y
);
624 (*m_History
)[m_HistoryPos
].SetPos(y
);
626 // go to previous position:
629 l
= (*m_History
)[m_HistoryPos
].GetPage();
630 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
633 if (a
== wxEmptyString
) LoadPage(l
);
634 else LoadPage(l
+ wxT("#") + a
);
637 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
642 bool wxHtmlWindow::HistoryCanBack()
644 if (m_HistoryPos
< 1) return FALSE
;
649 bool wxHtmlWindow::HistoryForward()
653 if (m_HistoryPos
== -1) return FALSE
;
654 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
656 m_OpenedPage
= wxEmptyString
; // this will disable adding new entry into history in LoadPage()
659 l
= (*m_History
)[m_HistoryPos
].GetPage();
660 a
= (*m_History
)[m_HistoryPos
].GetAnchor();
663 if (a
== wxEmptyString
) LoadPage(l
);
664 else LoadPage(l
+ wxT("#") + a
);
667 Scroll(0, (*m_History
)[m_HistoryPos
].GetPos());
672 bool wxHtmlWindow::HistoryCanForward()
674 if (m_HistoryPos
== -1) return FALSE
;
675 if (m_HistoryPos
>= (int)m_History
->GetCount() - 1)return FALSE
;
680 void wxHtmlWindow::HistoryClear()
686 void wxHtmlWindow::AddProcessor(wxHtmlProcessor
*processor
)
690 m_Processors
= new wxHtmlProcessorList
;
692 wxHtmlProcessorList::compatibility_iterator node
;
694 for (node
= m_Processors
->GetFirst(); node
; node
= node
->GetNext())
696 if (processor
->GetPriority() > node
->GetData()->GetPriority())
698 m_Processors
->Insert(node
, processor
);
702 m_Processors
->Append(processor
);
705 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor
*processor
)
707 if (!m_GlobalProcessors
)
709 m_GlobalProcessors
= new wxHtmlProcessorList
;
711 wxHtmlProcessorList::compatibility_iterator node
;
713 for (node
= m_GlobalProcessors
->GetFirst(); node
; node
= node
->GetNext())
715 if (processor
->GetPriority() > node
->GetData()->GetPriority())
717 m_GlobalProcessors
->Insert(node
, processor
);
721 m_GlobalProcessors
->Append(processor
);
726 wxList
wxHtmlWindow::m_Filters
;
727 wxHtmlFilter
*wxHtmlWindow::m_DefaultFilter
= NULL
;
728 wxHtmlProcessorList
*wxHtmlWindow::m_GlobalProcessors
= NULL
;
730 void wxHtmlWindow::CleanUpStatics()
732 wxDELETE(m_DefaultFilter
);
733 WX_CLEAR_LIST(wxList
, m_Filters
);
734 if (m_GlobalProcessors
)
735 WX_CLEAR_LIST(wxHtmlProcessorList
, *m_GlobalProcessors
);
736 wxDELETE(m_GlobalProcessors
);
741 void wxHtmlWindow::AddFilter(wxHtmlFilter
*filter
)
743 m_Filters
.Append(filter
);
747 bool wxHtmlWindow::IsSelectionEnabled() const
750 return !(m_Style
& wxHW_NO_SELECTION
);
758 wxString
wxHtmlWindow::DoSelectionToText(wxHtmlSelection
*sel
)
761 return wxEmptyString
;
765 const wxHtmlCell
*end
= sel
->GetToCell();
767 wxHtmlTerminalCellsInterator
i(sel
->GetFromCell(), end
);
770 text
<< i
->ConvertToText(sel
);
773 const wxHtmlCell
*prev
= *i
;
776 if ( prev
->GetParent() != i
->GetParent() )
778 text
<< i
->ConvertToText(*i
== end
? sel
: NULL
);
785 wxString
wxHtmlWindow::ToText()
790 sel
.Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
791 return DoSelectionToText(&sel
);
794 return wxEmptyString
;
797 #endif // wxUSE_CLIPBOARD
799 bool wxHtmlWindow::CopySelection(ClipboardType t
)
805 wxTheClipboard
->UsePrimarySelection(t
== Primary
);
807 // Primary selection exists only under X11, so don't do anything under
808 // the other platforms when we try to access it
810 // TODO: this should be abstracted at wxClipboard level!
813 #endif // __UNIX__/!__UNIX__
815 if ( wxTheClipboard
->Open() )
817 const wxString
txt(SelectionToText());
818 wxTheClipboard
->SetData(new wxTextDataObject(txt
));
819 wxTheClipboard
->Close();
820 wxLogTrace(_T("wxhtmlselection"),
821 _("Copied to clipboard:\"%s\""), txt
.c_str());
826 #endif // wxUSE_CLIPBOARD
832 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo
& link
)
834 const wxMouseEvent
*e
= link
.GetEvent();
835 if (e
== NULL
|| e
->LeftUp())
836 LoadPage(link
.GetHref());
839 void wxHtmlWindow::OnCellClicked(wxHtmlCell
*cell
,
840 wxCoord x
, wxCoord y
,
841 const wxMouseEvent
& event
)
843 wxCHECK_RET( cell
, _T("can't be called with NULL cell") );
845 cell
->OnMouseClick(this, x
, y
, event
);
848 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell
* WXUNUSED(cell
),
849 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
854 void wxHtmlWindow::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
858 void wxHtmlWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
862 if (m_tmpCanDrawLocks
> 0 || m_Cell
== NULL
) return;
865 GetViewStart(&x
, &y
);
866 wxRect rect
= GetUpdateRegion().GetBox();
867 wxSize sz
= GetSize();
871 m_backBuffer
= new wxBitmap(sz
.x
, sz
.y
);
872 dcm
.SelectObject(*m_backBuffer
);
873 dcm
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
876 dcm
.SetMapMode(wxMM_TEXT
);
877 dcm
.SetBackgroundMode(wxTRANSPARENT
);
879 wxHtmlRenderingInfo rinfo
;
880 wxDefaultHtmlRenderingStyle rstyle
;
881 rinfo
.SetSelection(m_selection
);
882 rinfo
.SetStyle(&rstyle
);
883 m_Cell
->Draw(dcm
, 0, 0,
884 y
* wxHTML_SCROLL_STEP
+ rect
.GetTop(),
885 y
* wxHTML_SCROLL_STEP
+ rect
.GetBottom(),
888 //#define DEBUG_HTML_SELECTION
889 #ifdef DEBUG_HTML_SELECTION
892 wxGetMousePosition(&xc
, &yc
);
893 ScreenToClient(&xc
, &yc
);
894 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
895 wxHtmlCell
*at
= m_Cell
->FindCellByPos(x
, y
);
897 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_BEFORE
);
899 m_Cell
->FindCellByPos(x
, y
, wxHTML_FIND_NEAREST_AFTER
);
901 dcm
.SetBrush(*wxTRANSPARENT_BRUSH
);
902 dcm
.SetPen(*wxBLACK_PEN
);
904 dcm
.DrawRectangle(at
->GetAbsPos(),
905 wxSize(at
->GetWidth(),at
->GetHeight()));
906 dcm
.SetPen(*wxGREEN_PEN
);
908 dcm
.DrawRectangle(before
->GetAbsPos().x
+1, before
->GetAbsPos().y
+1,
909 before
->GetWidth()-2,before
->GetHeight()-2);
910 dcm
.SetPen(*wxRED_PEN
);
912 dcm
.DrawRectangle(after
->GetAbsPos().x
+2, after
->GetAbsPos().y
+2,
913 after
->GetWidth()-4,after
->GetHeight()-4);
917 dcm
.SetDeviceOrigin(0,0);
918 dc
.Blit(0, rect
.GetTop(),
919 sz
.x
, rect
.GetBottom() - rect
.GetTop() + 1,
927 void wxHtmlWindow::OnSize(wxSizeEvent
& event
)
929 wxDELETE(m_backBuffer
);
931 wxScrolledWindow::OnSize(event
);
934 // Recompute selection if necessary:
937 m_selection
->Set(m_selection
->GetFromCell(),
938 m_selection
->GetToCell());
939 m_selection
->ClearPrivPos();
946 void wxHtmlWindow::OnMouseMove(wxMouseEvent
& WXUNUSED(event
))
948 m_tmpMouseMoved
= true;
951 void wxHtmlWindow::OnMouseDown(wxMouseEvent
& event
)
954 if ( event
.LeftDown() && IsSelectionEnabled() )
956 const long TRIPLECLICK_LEN
= 200; // 0.2 sec after doubleclick
957 if ( wxGetLocalTimeMillis() - m_lastDoubleClick
<= TRIPLECLICK_LEN
)
959 SelectLine(CalcUnscrolledPosition(event
.GetPosition()));
961 (void) CopySelection();
965 m_makingSelection
= true;
969 wxDELETE(m_selection
);
972 m_tmpSelFromPos
= CalcUnscrolledPosition(event
.GetPosition());
973 m_tmpSelFromCell
= NULL
;
978 #endif // wxUSE_CLIPBOARD
981 void wxHtmlWindow::OnMouseUp(wxMouseEvent
& event
)
984 if ( m_makingSelection
)
987 m_makingSelection
= false;
989 // did the user move the mouse far enough from starting point?
990 if ( CopySelection(Primary
) )
992 // we don't want mouse up event that ended selecting to be
993 // handled as mouse click and e.g. follow hyperlink:
997 #endif // wxUSE_CLIPBOARD
1002 wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
1003 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1005 // check is needed because FindCellByPos returns terminal cell and
1006 // containers may have empty borders -- in this case NULL will be
1009 OnCellClicked(cell
, pos
.x
, pos
.y
, event
);
1015 void wxHtmlWindow::OnInternalIdle()
1017 wxWindow::OnInternalIdle();
1019 if (m_tmpMouseMoved
&& (m_Cell
!= NULL
))
1021 #ifdef DEBUG_HTML_SELECTION
1025 wxGetMousePosition(&xc
, &yc
);
1026 ScreenToClient(&xc
, &yc
);
1027 CalcUnscrolledPosition(xc
, yc
, &x
, &y
);
1029 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(x
, y
);
1031 // handle selection update:
1032 if ( m_makingSelection
)
1034 if ( !m_tmpSelFromCell
)
1035 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1036 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
);
1038 // NB: a trick - we adjust selFromPos to be upper left or bottom
1039 // right corner of the first cell of the selection depending
1040 // on whether the mouse is moving to the right or to the left.
1041 // This gives us more "natural" behaviour when selecting
1042 // a line (specifically, first cell of the next line is not
1043 // included if you drag selection from left to right over
1046 if ( !m_tmpSelFromCell
)
1048 dirFromPos
= m_tmpSelFromPos
;
1052 dirFromPos
= m_tmpSelFromCell
->GetAbsPos();
1053 if ( x
< m_tmpSelFromPos
.x
)
1055 dirFromPos
.x
+= m_tmpSelFromCell
->GetWidth();
1056 dirFromPos
.y
+= m_tmpSelFromCell
->GetHeight();
1059 bool goingDown
= dirFromPos
.y
< y
||
1060 (dirFromPos
.y
== y
&& dirFromPos
.x
< x
);
1062 // determine selection span:
1063 if ( /*still*/ !m_tmpSelFromCell
)
1067 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1068 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1069 wxHTML_FIND_NEAREST_AFTER
);
1070 if (!m_tmpSelFromCell
)
1071 m_tmpSelFromCell
= m_Cell
->GetFirstTerminal();
1075 m_tmpSelFromCell
= m_Cell
->FindCellByPos(
1076 m_tmpSelFromPos
.x
,m_tmpSelFromPos
.y
,
1077 wxHTML_FIND_NEAREST_BEFORE
);
1078 if (!m_tmpSelFromCell
)
1079 m_tmpSelFromCell
= m_Cell
->GetLastTerminal();
1083 wxHtmlCell
*selcell
= cell
;
1088 selcell
= m_Cell
->FindCellByPos(x
, y
,
1089 wxHTML_FIND_NEAREST_BEFORE
);
1091 selcell
= m_Cell
->GetLastTerminal();
1095 selcell
= m_Cell
->FindCellByPos(x
, y
,
1096 wxHTML_FIND_NEAREST_AFTER
);
1098 selcell
= m_Cell
->GetFirstTerminal();
1102 // NB: it may *rarely* happen that the code above didn't find one
1103 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1105 if ( selcell
&& m_tmpSelFromCell
)
1109 // start selecting only if mouse movement was big enough
1110 // (otherwise it was meant as mouse click, not selection):
1111 const int PRECISION
= 2;
1112 wxPoint diff
= m_tmpSelFromPos
- wxPoint(x
,y
);
1113 if (abs(diff
.x
) > PRECISION
|| abs(diff
.y
) > PRECISION
)
1115 m_selection
= new wxHtmlSelection();
1120 if ( m_tmpSelFromCell
->IsBefore(selcell
) )
1122 m_selection
->Set(m_tmpSelFromPos
, m_tmpSelFromCell
,
1123 wxPoint(x
,y
), selcell
); }
1126 m_selection
->Set(wxPoint(x
,y
), selcell
,
1127 m_tmpSelFromPos
, m_tmpSelFromCell
);
1129 m_selection
->ClearPrivPos();
1135 // handle cursor and status bar text changes:
1136 if ( cell
!= m_tmpLastCell
)
1138 wxHtmlLinkInfo
*lnk
= cell
? cell
->GetLink(x
, y
) : NULL
;
1141 cur
= cell
->GetCursor();
1143 cur
= *wxSTANDARD_CURSOR
;
1146 if (lnk
!= m_tmpLastLink
)
1151 if (m_RelatedStatusBar
!= -1)
1152 m_RelatedFrame
->SetStatusText(wxEmptyString
,
1153 m_RelatedStatusBar
);
1157 if (m_RelatedStatusBar
!= -1)
1158 m_RelatedFrame
->SetStatusText(lnk
->GetHref(),
1159 m_RelatedStatusBar
);
1161 #endif // wxUSE_STATUSBAR
1162 m_tmpLastLink
= lnk
;
1165 m_tmpLastCell
= cell
;
1167 else // mouse moved but stayed in the same cell
1170 OnCellMouseHover(cell
, x
, y
);
1173 m_tmpMouseMoved
= FALSE
;
1178 void wxHtmlWindow::StopAutoScrolling()
1180 if ( m_timerAutoScroll
)
1182 wxDELETE(m_timerAutoScroll
);
1186 void wxHtmlWindow::OnMouseEnter(wxMouseEvent
& event
)
1188 StopAutoScrolling();
1192 void wxHtmlWindow::OnMouseLeave(wxMouseEvent
& event
)
1194 // don't prevent the usual processing of the event from taking place
1197 // when a captured mouse leave a scrolled window we start generate
1198 // scrolling events to allow, for example, extending selection beyond the
1199 // visible area in some controls
1200 if ( wxWindow::GetCapture() == this )
1202 // where is the mouse leaving?
1204 wxPoint pt
= event
.GetPosition();
1207 orient
= wxHORIZONTAL
;
1210 else if ( pt
.y
< 0 )
1212 orient
= wxVERTICAL
;
1215 else // we're lower or to the right of the window
1217 wxSize size
= GetClientSize();
1218 if ( pt
.x
> size
.x
)
1220 orient
= wxHORIZONTAL
;
1221 pos
= GetVirtualSize().x
/ wxHTML_SCROLL_STEP
;
1223 else if ( pt
.y
> size
.y
)
1225 orient
= wxVERTICAL
;
1226 pos
= GetVirtualSize().y
/ wxHTML_SCROLL_STEP
;
1228 else // this should be impossible
1230 // but seems to happen sometimes under wxMSW - maybe it's a bug
1231 // there but for now just ignore it
1233 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1239 // only start the auto scroll timer if the window can be scrolled in
1241 if ( !HasScrollbar(orient
) )
1244 delete m_timerAutoScroll
;
1245 m_timerAutoScroll
= new wxHtmlWinAutoScrollTimer
1248 pos
== 0 ? wxEVT_SCROLLWIN_LINEUP
1249 : wxEVT_SCROLLWIN_LINEDOWN
,
1253 m_timerAutoScroll
->Start(50); // FIXME: make configurable
1257 void wxHtmlWindow::OnKeyUp(wxKeyEvent
& event
)
1259 if ( IsSelectionEnabled() &&
1260 event
.GetKeyCode() == 'C' && event
.ControlDown() )
1262 (void) CopySelection();
1266 void wxHtmlWindow::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1268 (void) CopySelection();
1271 void wxHtmlWindow::OnDoubleClick(wxMouseEvent
& event
)
1273 // select word under cursor:
1274 if ( IsSelectionEnabled() )
1276 SelectWord(CalcUnscrolledPosition(event
.GetPosition()));
1278 (void) CopySelection(Primary
);
1280 m_lastDoubleClick
= wxGetLocalTimeMillis();
1286 void wxHtmlWindow::SelectWord(const wxPoint
& pos
)
1290 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1294 m_selection
= new wxHtmlSelection();
1295 m_selection
->Set(cell
, cell
);
1296 RefreshRect(wxRect(CalcScrolledPosition(cell
->GetAbsPos()),
1297 wxSize(cell
->GetWidth(), cell
->GetHeight())));
1302 void wxHtmlWindow::SelectLine(const wxPoint
& pos
)
1306 wxHtmlCell
*cell
= m_Cell
->FindCellByPos(pos
.x
, pos
.y
);
1309 // We use following heuristic to find a "line": let the line be all
1310 // cells in same container as the cell under mouse cursor that are
1311 // neither completely above nor completely bellow the clicked cell
1312 // (i.e. are likely to be words positioned on same line of text).
1314 int y1
= cell
->GetAbsPos().y
;
1315 int y2
= y1
+ cell
->GetHeight();
1317 const wxHtmlCell
*c
;
1318 const wxHtmlCell
*before
= NULL
;
1319 const wxHtmlCell
*after
= NULL
;
1321 // find last cell of line:
1322 for ( c
= cell
->GetNext(); c
; c
= c
->GetNext())
1324 y
= c
->GetAbsPos().y
;
1325 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1333 // find first cell of line:
1334 for ( c
= cell
->GetParent()->GetFirstChild();
1335 c
&& c
!= cell
; c
= c
->GetNext())
1337 y
= c
->GetAbsPos().y
;
1338 if ( y
+ c
->GetHeight() > y1
&& y
< y2
)
1350 m_selection
= new wxHtmlSelection();
1351 m_selection
->Set(before
, after
);
1358 void wxHtmlWindow::SelectAll()
1363 m_selection
= new wxHtmlSelection();
1364 m_selection
->Set(m_Cell
->GetFirstTerminal(), m_Cell
->GetLastTerminal());
1369 #endif // wxUSE_CLIPBOARD
1373 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor
,wxObject
)
1375 #if wxUSE_EXTENDED_RTTI
1376 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow
, wxScrolledWindow
,"wx/html/htmlwin.h")
1378 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow
)
1381 style , wxHW_SCROLLBAR_AUTO
1382 borders , (dimension)
1386 wxEND_PROPERTIES_TABLE()
1388 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow
)
1389 wxEND_HANDLERS_TABLE()
1391 wxCONSTRUCTOR_5( wxHtmlWindow
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1393 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow
,wxScrolledWindow
)
1396 BEGIN_EVENT_TABLE(wxHtmlWindow
, wxScrolledWindow
)
1397 EVT_SIZE(wxHtmlWindow::OnSize
)
1398 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown
)
1399 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp
)
1400 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp
)
1401 EVT_MOTION(wxHtmlWindow::OnMouseMove
)
1402 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground
)
1403 EVT_PAINT(wxHtmlWindow::OnPaint
)
1405 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick
)
1406 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter
)
1407 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave
)
1408 EVT_KEY_UP(wxHtmlWindow::OnKeyUp
)
1409 EVT_MENU(wxID_COPY
, wxHtmlWindow::OnCopy
)
1410 #endif // wxUSE_CLIPBOARD
1417 // A module to allow initialization/cleanup
1418 // without calling these functions from app.cpp or from
1419 // the user's application.
1421 class wxHtmlWinModule
: public wxModule
1423 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule
)
1425 wxHtmlWinModule() : wxModule() {}
1426 bool OnInit() { return TRUE
; }
1427 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1430 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule
, wxModule
)
1433 // This hack forces the linker to always link in m_* files
1434 // (wxHTML doesn't work without handlers from these files)
1435 #include "wx/html/forcelnk.h"
1436 FORCE_WXHTML_MODULES()
1438 #endif // wxUSE_HTML