From 184144794d383a1ddd6167443f6c22a3281aa96c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 14 Nov 2001 01:27:16 +0000 Subject: [PATCH] added wxTextCtrl::GetSelection() returning a wxString - useful for multiline controls under Windows git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12408 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/text.tex | 14 ++++++++++++++ include/wx/msw/textctrl.h | 2 +- include/wx/textctrl.h | 2 ++ samples/text/text.cpp | 12 ++++++++++++ src/common/textcmn.cpp | 14 ++++++++++++++ src/msw/textctrl.cpp | 28 ++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex index 8d26a475c3..8bb889ce5d 100644 --- a/docs/latex/wx/text.tex +++ b/docs/latex/wx/text.tex @@ -368,6 +368,13 @@ working with controls that contain large amounts of text. Gets the current selection span. If the returned values are equal, there was no selection. +Please note that the indices returned may be used with the other wxTextctrl +methods but don't necessarily represent the correct indices into the string +returned by \helpref{GetValue()}{wxtextctrlgetvalue} for multiline controls +under Windows, you should use another version of +\helpref{GetSelection()}{wxtextctrlgetselectionstring} to get the selected +text. + \wxheading{Parameters} \docparam{from}{The returned first position.} @@ -380,6 +387,13 @@ consisting of the from and to values.} \perlnote{In wxPerl this method takes no parameter and returns a 2-element list {\tt ( from, to )}.} +\membersection{wxTextCtrl::GetSelection}\label{wxtextctrlgetselectionstring} + +\func{virtual wxString}{GetSelection}{\void} + +Gets the text currently selected in the control. If there is no selection, the +returned string is empty. + \membersection{wxTextCtrl::GetValue}\label{wxtextctrlgetvalue} \constfunc{wxString}{GetValue}{\void} diff --git a/include/wx/msw/textctrl.h b/include/wx/msw/textctrl.h index a57f54c754..3e0174e66e 100644 --- a/include/wx/msw/textctrl.h +++ b/include/wx/msw/textctrl.h @@ -55,8 +55,8 @@ public: virtual bool IsModified() const; virtual bool IsEditable() const; - // If the return values from and to are the same, there is no selection. virtual void GetSelection(long* from, long* to) const; + virtual wxString GetSelection() const; // operations // ---------- diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index 3ecf9e17f9..98155be307 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -157,6 +157,8 @@ public: // If the return values from and to are the same, there is no selection. virtual void GetSelection(long* from, long* to) const = 0; + virtual wxString GetSelection() const; + // operations // ---------- diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 3872f7cccc..456e42c53d 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -685,6 +685,18 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event) case WXK_F7: ShowPosition(10); break; + + case WXK_F10: + { + long from, to; + GetSelection(&from, &to); + + wxString sel = GetSelection(); + + wxLogMessage(_T("Selection: from %ld to %ld."), from, to); + wxLogMessage(_T("Selection = '%s' (len = %u)"), + sel.c_str(), sel.length()); + } } if ( ms_logKey ) diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index b2e57eb854..0ada7cb2f2 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -244,6 +244,20 @@ void wxTextCtrlBase::SelectAll() SetSelection(0, GetLastPosition()); } +wxString wxTextCtrlBase::GetSelection() const +{ + long from, to; + GetSelection(&from, &to); + + wxString sel; + if ( from < to ) + { + sel = GetValue().Mid(from, to - from); + } + + return sel; +} + #else // !wxUSE_TEXTCTRL // define this one even if !wxUSE_TEXTCTRL because it is also used by other diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index bc78ce19b0..7d13ae10f4 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -648,6 +648,34 @@ void wxTextCtrl::GetSelection(long* from, long* to) const } } +wxString wxTextCtrl::GetSelection() const +{ + // the base class version works correctly for the rich text controls + // because there the lines are terminated with just '\r' which means that + // the string length is not changed in the result of the translations doen + // in GetValue() but for the normal ones when we replace "\r\n" with '\n' + // we break the indices +#if wxUSE_RICHEDIT + if ( m_isRich ) + return wxTextCtrlBase::GetSelection(); +#endif // wxUSE_RICHEDIT + + long from, to; + GetSelection(&from, &to); + + wxString str; + if ( from < to ) + { + str = wxGetWindowText(GetHWND()).Mid(from, to - from); + + // and now that we have the correct selection, convert it to the + // correct format + str = wxTextFile::Translate(str, wxTextFileType_Unix); + } + + return str; +} + bool wxTextCtrl::IsEditable() const { long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); -- 2.45.2