+wxTextCtrlHitTestResult
+wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const
+{
+ // first get the position from Windows
+ LPARAM lParam;
+
+#if wxUSE_RICHEDIT
+ POINTL ptl;
+ if ( IsRich() )
+ {
+ // for rich edit controls the position is passed iva the struct fields
+ ptl.x = pt.x;
+ ptl.y = pt.y;
+ lParam = (LPARAM)&ptl;
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ // for the plain ones, we are limited to 16 bit positions which are
+ // combined in a single 32 bit value
+ lParam = MAKELPARAM(pt.x, pt.y);
+ }
+
+ LRESULT pos = ::SendMessage(GetHwnd(), EM_CHARFROMPOS, 0, lParam);
+
+ if ( pos == -1 )
+ {
+ // this seems to indicate an error...
+ return wxTE_HT_UNKNOWN;
+ }
+
+#if wxUSE_RICHEDIT
+ if ( !IsRich() )
+#endif // wxUSE_RICHEDIT
+ {
+ // for plain EDIT controls the higher word contains something else
+ pos = LOWORD(pos);
+ }
+
+
+ // next determine where it is relatively to our point: EM_CHARFROMPOS
+ // always returns the closest character but we need to be more precise, so
+ // double check that we really are where it pretends
+ POINTL ptReal;
+
+#if wxUSE_RICHEDIT
+ // FIXME: we need to distinguish between richedit 2 and 3 here somehow but
+ // we don't know how to do it
+ if ( IsRich() )
+ {
+ ::SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&ptReal, pos);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ LRESULT lRc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0);
+
+ if ( lRc == -1 )
+ {
+ // this is apparently returned when pos corresponds to the last
+ // position
+ ptReal.x =
+ ptReal.y = 0;
+ }
+ else
+ {
+ ptReal.x = LOWORD(lRc);
+ ptReal.y = HIWORD(lRc);
+ }
+ }
+
+ wxTextCtrlHitTestResult rc;
+
+ if ( pt.y > ptReal.y + GetCharHeight() )
+ rc = wxTE_HT_BELOW;
+ else if ( pt.x > ptReal.x + GetCharWidth() )
+ rc = wxTE_HT_BEYOND;
+ else
+ rc = wxTE_HT_ON_TEXT;
+
+ if ( posOut )
+ *posOut = pos;
+
+ return rc;
+}
+
+// ----------------------------------------------------------------------------
+//
+// ----------------------------------------------------------------------------
+