+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;
+}
+
+wxPoint wxTextCtrl::DoPositionToCoords(long pos) const
+{
+ // FIXME: This code is broken for rich edit version 2.0 as it uses the same
+ // API as plain edit i.e. the coordinates are returned directly instead of
+ // filling the POINT passed as WPARAM with them but we can't distinguish
+ // between 2.0 and 3.0 unfortunately (see also the use of EM_POSFROMCHAR
+ // above).
+#if wxUSE_RICHEDIT
+ if ( IsRich() )
+ {
+ POINT pt;
+ LRESULT rc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, (WPARAM)&pt, pos);
+ if ( rc != -1 )
+ return wxPoint(pt.x, pt.y);
+ }
+ else
+#endif // wxUSE_RICHEDIT
+ {
+ LRESULT rc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos, 0);
+ if ( rc == -1 )
+ {
+ // Finding coordinates for the last position of the control fails
+ // in plain EDIT control, try to compensate for it by finding it
+ // ourselves from the position of the previous character.
+ if ( pos < GetLastPosition() )
+ {
+ // It's not the expected correctable failure case so just fail.
+ return wxDefaultPosition;
+ }
+
+ if ( pos == 0 )
+ {
+ // We're being asked the coordinates of the first (and last and
+ // only) position in an empty control. There is no way to get
+ // it directly with EM_POSFROMCHAR but EM_GETMARGINS returns
+ // the correct value for at least the horizontal offset.
+ rc = ::SendMessage(GetHwnd(), EM_GETMARGINS, 0, 0);
+
+ // Text control seems to effectively add 1 to margin.
+ return wxPoint(LOWORD(rc) + 1, 1);
+ }
+
+ // We do have a previous character, try to get its coordinates.
+ rc = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, pos - 1, 0);
+ if ( rc == -1 )
+ {
+ // If getting coordinates of the previous character failed as
+ // well, just give up.
+ return wxDefaultPosition;
+ }
+
+ wxString prevChar = GetRange(pos - 1, pos);
+ wxSize prevCharSize = GetTextExtent(prevChar);
+
+ if ( prevChar == wxT("\n" ))
+ {
+ // 'pos' is at the beginning of a new line so its X coordinate
+ // should be the same as X coordinate of the first character of
+ // any other line while its Y coordinate will be approximately
+ // (but we can't compute it exactly...) one character height
+ // more than that of the previous character.
+ LRESULT coords0 = ::SendMessage(GetHwnd(), EM_POSFROMCHAR, 0, 0);
+ if ( coords0 == -1 )
+ return wxDefaultPosition;
+
+ rc = MAKELPARAM(LOWORD(coords0), HIWORD(rc) + prevCharSize.y);
+ }
+ else
+ {
+ // Simple case: previous character is in the same line so this
+ // one is just after it.
+ rc += MAKELPARAM(prevCharSize.x, 0);
+ }
+ }
+
+ // Notice that {LO,HI}WORD macros return WORDs, i.e. unsigned shorts,
+ // while we want to have signed values here (the y coordinate of any
+ // position above the first currently visible line is negative, for
+ // example), hence the need for casts.
+ return wxPoint(static_cast<short>(LOWORD(rc)),
+ static_cast<short>(HIWORD(rc)));
+ }
+
+ return wxDefaultPosition;
+}
+
+
+// ----------------------------------------------------------------------------
+//
+// ----------------------------------------------------------------------------
+