X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9ffa72272ba67827a83993b1e3ec8a312ec88434..d223107206e4dfa6768e0bc2455325dd49e4d2bb:/src/msw/textctrl.cpp diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index d577edd1a2..fa977a9896 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -564,9 +564,8 @@ wxString wxTextCtrl::GetRange(long from, long to) const if ( !str.empty() ) { // we have to manually extract the required part, luckily - // this is easy in this case as EOL characters in richedit - // version > 1 are just '\r's and so positions map to - // string indices one to one (unlike with richedit 1) + // this is easy in this case as EOL characters in str are + // just LFs because we remove CRs in wxRichEditStreamOut str = str.Mid(from, to - from); } } @@ -638,6 +637,9 @@ void wxTextCtrl::SetValue(const wxString& value) if ( (value.length() > 0x400) || (value != GetValue()) ) { DoWriteText(value, FALSE /* not selection only */); + + // for compatibility, don't move the cursor when doing SetValue() + SetInsertionPoint(0); } else // same text { @@ -650,9 +652,6 @@ void wxTextCtrl::SetValue(const wxString& value) // mark the control as being not dirty - we changed its text, not the // user DiscardEdits(); - - // for compatibility, don't move the cursor when doing SetValue() - SetInsertionPoint(0); } #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU) @@ -681,24 +680,37 @@ wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) return 0; } +// helper struct used to pass parameters from wxTextCtrl to wxRichEditStreamOut +struct wxStreamOutData +{ + wchar_t *wpc; + size_t len; +}; + DWORD CALLBACK -wxRichEditStreamOut(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) +wxRichEditStreamOut(DWORD_PTR dwCookie, BYTE *buf, LONG cb, LONG *pcb) { *pcb = 0; - wchar_t ** const ppws = (wchar_t **)dwCookie; + wxStreamOutData *data = (wxStreamOutData *)dwCookie; const wchar_t *wbuf = (const wchar_t *)buf; - wchar_t *wpc = *ppws; - while ( cb && *wpc ) + wchar_t *wpc = data->wpc; + while ( cb ) { - *wpc++ = *wbuf++; + wchar_t wch = *wbuf++; + + // turn "\r\n" into "\n" on the fly + if ( wch != L'\r' ) + *wpc++ = wch; + else + data->len--; cb -= sizeof(wchar_t); (*pcb) += sizeof(wchar_t); } - *ppws = wpc; + data->wpc = wpc; return 0; } @@ -783,9 +795,13 @@ wxTextCtrl::StreamOut(wxFontEncoding encoding, bool selectionOnly) const wchar_t *wpc = wchBuf; #endif + wxStreamOutData data; + data.wpc = wpc; + data.len = len; + EDITSTREAM eds; wxZeroMemory(eds); - eds.dwCookie = (DWORD)&wpc; + eds.dwCookie = (DWORD)&data; eds.pfnCallback = wxRichEditStreamOut; ::SendMessage @@ -802,13 +818,17 @@ wxTextCtrl::StreamOut(wxFontEncoding encoding, bool selectionOnly) const } else // streamed out ok { - // now convert to the given encoding (this is a lossful conversion but - // what else can we do) + // NUL-terminate the string because its length could have been + // decreased by wxRichEditStreamOut + *(wchBuf.data() + data.len) = L'\0'; + + // now convert to the given encoding (this is a possibly lossful + // conversion but what else can we do) wxCSConv conv(encoding); - size_t lenNeeded = conv.WC2MB(NULL, wchBuf, len); - if ( lenNeeded ) + size_t lenNeeded = conv.WC2MB(NULL, wchBuf, 0); + if ( lenNeeded++ ) { - conv.WC2MB(wxStringBuffer(out, lenNeeded), wchBuf, len); + conv.WC2MB(wxStringBuffer(out, lenNeeded), wchBuf, lenNeeded); } } @@ -1236,6 +1256,23 @@ void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret) #endif // Win32/16 } +// ---------------------------------------------------------------------------- +// Working with files +// ---------------------------------------------------------------------------- + +bool wxTextCtrl::LoadFile(const wxString& file) +{ + if ( wxTextCtrlBase::LoadFile(file) ) + { + // update the size limit if needed + AdjustSpaceLimit(); + + return TRUE; + } + + return FALSE; +} + // ---------------------------------------------------------------------------- // Editing // ---------------------------------------------------------------------------- @@ -1253,19 +1290,6 @@ void wxTextCtrl::Remove(long from, long to) Replace(from, to, wxEmptyString); } -bool wxTextCtrl::LoadFile(const wxString& file) -{ - if ( wxTextCtrlBase::LoadFile(file) ) - { - // update the size limit if needed - AdjustSpaceLimit(); - - return TRUE; - } - - return FALSE; -} - bool wxTextCtrl::IsModified() const { return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0; @@ -1286,6 +1310,10 @@ int wxTextCtrl::GetNumberOfLines() const return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0); } +// ---------------------------------------------------------------------------- +// Positions <-> coords +// ---------------------------------------------------------------------------- + long wxTextCtrl::XYToPosition(long x, long y) const { // This gets the char index for the _beginning_ of this line @@ -1333,6 +1361,99 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const return TRUE; } +wxTextCtrlHitTestResult +wxTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *col, wxTextCoord *row) 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; + + // finally translate to column/row + if ( !PositionToXY(pos, col, row) ) + { + wxFAIL_MSG( _T("PositionToXY() not expected to fail in HitTest()") ); + } + + return rc; +} + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- + void wxTextCtrl::ShowPosition(long pos) { HWND hWnd = GetHwnd(); @@ -1558,7 +1679,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) switch ( event.GetKeyCode() ) { case WXK_RETURN: - if ( !(m_windowStyle & wxTE_MULTILINE) ) + if ( !HasFlag(wxTE_MULTILINE) ) { wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); InitCommandEvent(event); @@ -1571,10 +1692,26 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) break; case WXK_TAB: - // always produce navigation event - even if we process TAB + // always produce navigation event -- even if we process TAB // ourselves the fact that we got here means that the user code - // decided to skip processing of this TAB - probably to let it + // decided to skip processing of this TAB -- probably to let it // do its default job. + + // ok, so this is getting absolutely ridiculous but I don't see + // any other way to fix this bug: when a multiline text control is + // inside a wxFrame, we need to generate the navigation event as + // otherwise nothing happens at all, but when the same control is + // created inside a dialog, IsDialogMessage() *does* switch focus + // all by itself and so if we do it here as well, it is advanced + // twice and goes to the next control... to prevent this from + // happening we're doing this ugly check, the logic being that if + // we don't have focus then it had been already changed to the next + // control + // + // the right thing to do would, of course, be to understand what + // the hell is IsDialogMessage() doing but this is beyond my feeble + // forces at the moment unfortunately + if ( FindFocus() == this ) { wxNavigationKeyEvent eventNav; eventNav.SetDirection(!event.ShiftDown()); @@ -1591,9 +1728,9 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) event.Skip(); } -long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) +WXLRESULT wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) { - long lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); + WXLRESULT lRc = wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam); if ( nMsg == WM_GETDLGCODE ) { @@ -1826,13 +1963,17 @@ wxSize wxTextCtrl::DoGetBestSize() const int wText = DEFAULT_ITEM_WIDTH; - int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); + int hText = cy; if ( m_windowStyle & wxTE_MULTILINE ) { hText *= wxMax(GetNumberOfLines(), 5); } //else: for single line control everything is ok + // we have to add the adjustments for the control height only once, not + // once per line, so do it after multiplication above + hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy; + return wxSize(wText, hText); }