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
This commit is contained in:
Vadim Zeitlin
2001-11-14 01:27:16 +00:00
parent 9327c3aa02
commit 184144794d
6 changed files with 71 additions and 1 deletions

View File

@@ -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 Gets the current selection span. If the returned values are equal, there was
no selection. 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} \wxheading{Parameters}
\docparam{from}{The returned first position.} \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 \perlnote{In wxPerl this method takes no parameter and returns a
2-element list {\tt ( from, to )}.} 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} \membersection{wxTextCtrl::GetValue}\label{wxtextctrlgetvalue}
\constfunc{wxString}{GetValue}{\void} \constfunc{wxString}{GetValue}{\void}

View File

@@ -55,8 +55,8 @@ public:
virtual bool IsModified() const; virtual bool IsModified() const;
virtual bool IsEditable() 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 void GetSelection(long* from, long* to) const;
virtual wxString GetSelection() const;
// operations // operations
// ---------- // ----------

View File

@@ -157,6 +157,8 @@ public:
// If the return values from and to are the same, there is no selection. // If the return values from and to are the same, there is no selection.
virtual void GetSelection(long* from, long* to) const = 0; virtual void GetSelection(long* from, long* to) const = 0;
virtual wxString GetSelection() const;
// operations // operations
// ---------- // ----------

View File

@@ -685,6 +685,18 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
case WXK_F7: case WXK_F7:
ShowPosition(10); ShowPosition(10);
break; 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 ) if ( ms_logKey )

View File

@@ -244,6 +244,20 @@ void wxTextCtrlBase::SelectAll()
SetSelection(0, GetLastPosition()); 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 #else // !wxUSE_TEXTCTRL
// define this one even if !wxUSE_TEXTCTRL because it is also used by other // define this one even if !wxUSE_TEXTCTRL because it is also used by other

View File

@@ -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 bool wxTextCtrl::IsEditable() const
{ {
long style = ::GetWindowLong(GetHwnd(), GWL_STYLE); long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);