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.}
\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}
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
// ----------
// 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
// ----------
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 )
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
}
}
+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);