From: Vadim Zeitlin Date: Fri, 23 Jul 2004 18:07:40 +0000 (+0000) Subject: implemented HitTest() for GTK2; test it in the sample X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/692c9b869686c97963c693e99efba19edeaba8cc implemented HitTest() for GTK2; test it in the sample git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28430 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 2b8bf2bd60..9f7158bd61 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -226,16 +226,17 @@ Unix: wxGTK: +- wxGTK uses GTK+ 2.x by default now, you have to pass --disable-gtk2 to + configure if you want to use GTK+ 1.2 - fixed many rendering artifacts and wrong colours with lots of GTK+ themes - implemented wxColourDialog as native dialog +- implemented wxTextCtrl::HitTest() (GTK+ >= 2) - wxTreeCtrl::GetCount() counts root as well now (compatible with MSW) - added support for wxCHK_3STATE style (GTK2 only) - implemented text underlining under GTK2 - implemented wxFRAME_NO_TASKBAR style (GTK >= 2.2) - implemented support for wxSYS_DCLICK_?, wxSYS_DRAG_? and wxSYS_CURSOR_? in wxSystemSettings::GetMetric (Mart Raudsepp) -- wxGTK uses GTK+ 2.x by default now, you have to pass --disable-gtk2 to - configure if you want to use GTK+ 1.2 - implemented wxTopLevel::IsMaximized() for GTK+2 and WMs that implement freedesktop.org's wm-spec (Mart Raudsepp) diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex index dbb25cdce2..93129cdabc 100644 --- a/docs/latex/wx/text.tex +++ b/docs/latex/wx/text.tex @@ -825,8 +825,8 @@ pixels. If the return code is not \texttt{wxTE\_HT\_UNKNOWN} the row and column of the character closest to this position are returned in the \arg{col} and \arg{row} parameters (unless the pointers are {\tt NULL} which is allowed). -Please note that this function is currently only implemented in wxUniv and -wxMSW ports. +Please note that this function is currently only implemented in wxUniv, +wxMSW and wxGTK2 ports. \wxheading{See also} diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index e8f7cc8df3..e93198c307 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -330,6 +330,7 @@ public: // // NB: pt is in device coords (not adjusted for the client area origin nor // scrolling) + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const; virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, wxTextCoord *col, wxTextCoord *row) const; diff --git a/include/wx/univ/textctrl.h b/include/wx/univ/textctrl.h index eba0ae2314..e756e912e3 100644 --- a/include/wx/univ/textctrl.h +++ b/include/wx/univ/textctrl.h @@ -191,6 +191,7 @@ public: void RemoveSelection(); wxString GetSelectionText() const; + virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const; virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, wxTextCoord *col, wxTextCoord *row) const; diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 4e3fb60c54..40117ebde4 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -662,15 +662,24 @@ void MyTextCtrl::OnMouseEvent(wxMouseEvent& ev) msg = GetMouseEventDesc(ev); } - msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") ") - << _T("Flags: ") + msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") "); + + long pos; + wxTextCtrlHitTestResult rc = HitTest(ev.GetPosition(), &pos); + if ( rc != wxTE_HT_UNKNOWN ) + { + msg << _T("at position ") << pos; + } + + msg << _T("[Flags: ") << GetChar( ev.LeftIsDown(), _T('1') ) << GetChar( ev.MiddleIsDown(), _T('2') ) << GetChar( ev.RightIsDown(), _T('3') ) << GetChar( ev.ControlDown(), _T('C') ) << GetChar( ev.AltDown(), _T('A') ) << GetChar( ev.ShiftDown(), _T('S') ) - << GetChar( ev.MetaDown(), _T('M') ); + << GetChar( ev.MetaDown(), _T('M') ) + << _T(']'); wxLogMessage(msg); } diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index 98bec4e726..1b2b1f1532 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -495,10 +495,26 @@ void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event) // hit testing // ---------------------------------------------------------------------------- +wxTextCtrlHitTestResult +wxTextCtrlBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const +{ + // implement in terms of the other overload as the native ports typically + // can get the position and not (x, y) pair directly (although wxUniv + // directly gets x and y -- and so overrides this method as well) + long pos; + wxTextCtrlHitTestResult rc = HitTest(pt, &pos); + + if ( rc != wxTE_HT_UNKNOWN ) + { + PositionToXY(pos, x, y); + } + + return rc; +} + wxTextCtrlHitTestResult wxTextCtrlBase::HitTest(const wxPoint& WXUNUSED(pt), - wxTextCoord * WXUNUSED(col), - wxTextCoord * WXUNUSED(row)) const + long * WXUNUSED(pos)) const { // not implemented return wxTE_HT_UNKNOWN; diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index ced9cf41e8..9def3e02e4 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -1057,6 +1057,36 @@ void wxTextCtrl::ShowPosition( long pos ) } } +#ifdef __WXGTK20__ + +wxTextCtrlHitTestResult +wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const +{ + if ( !IsMultiLine() ) + { + // not supported + return wxTE_HT_UNKNOWN; + } + + int x, y; + gtk_text_view_window_to_buffer_coords + ( + GTK_TEXT_VIEW(m_text), + GTK_TEXT_WINDOW_TEXT, + pt.x, pt.y, + &x, &y + ); + + GtkTextIter iter; + gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); + if ( pos ) + *pos = gtk_text_iter_get_offset(&iter); + + return wxTE_HT_ON_TEXT; +} + +#endif // __WXGTK20__ + long wxTextCtrl::GetInsertionPoint() const { wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); diff --git a/src/gtk1/textctrl.cpp b/src/gtk1/textctrl.cpp index ced9cf41e8..9def3e02e4 100644 --- a/src/gtk1/textctrl.cpp +++ b/src/gtk1/textctrl.cpp @@ -1057,6 +1057,36 @@ void wxTextCtrl::ShowPosition( long pos ) } } +#ifdef __WXGTK20__ + +wxTextCtrlHitTestResult +wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const +{ + if ( !IsMultiLine() ) + { + // not supported + return wxTE_HT_UNKNOWN; + } + + int x, y; + gtk_text_view_window_to_buffer_coords + ( + GTK_TEXT_VIEW(m_text), + GTK_TEXT_WINDOW_TEXT, + pt.x, pt.y, + &x, &y + ); + + GtkTextIter iter; + gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); + if ( pos ) + *pos = gtk_text_iter_get_offset(&iter); + + return wxTE_HT_ON_TEXT; +} + +#endif // __WXGTK20__ + long wxTextCtrl::GetInsertionPoint() const { wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); diff --git a/src/univ/textctrl.cpp b/src/univ/textctrl.cpp index 3b15ef1070..3f6168f230 100644 --- a/src/univ/textctrl.cpp +++ b/src/univ/textctrl.cpp @@ -2952,6 +2952,18 @@ wxTextCtrlHitTestResult wxTextCtrl::HitTestLine(const wxString& line, return res; } +wxTextCtrlHitTestResult wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const +{ + wxTextCoord x, y; + wxTextCtrlHitTestResult rc = HitTest(pt, &x, &y); + if ( rc != wxTE_HT_UNKNOWN && pos ) + { + *pos = XYToPosition(x, y); + } + + return rc; +} + wxTextCtrlHitTestResult wxTextCtrl::HitTest(const wxPoint& pos, wxTextCoord *colOut, wxTextCoord *rowOut) const