A text control allows text to be displayed and edited. It may be single line or multi-line.
Derived from
streambuf
wxControl
wxWindow
wxEvtHandler
wxObject
Include files
<wx/textctrl.h>
Window styles
wxTE_PROCESS_ENTER | The callback function will receive the message wxEVENT_TYPE_TEXT_ENTER_COMMAND. Note that this will break tab traversal for this panel item under Windows. |
wxTE_MULTILINE | The text control allows multiple lines. |
wxTE_PASSWORD | The text will be echoed as asterisks. |
wxTE_READONLY | The text will not be user-editable. |
wxHSCROLL | A horizontal scrollbar will be created. |
See also window styles overview and wxTextCtrl::wxTextCtrl.
Remarks
This class multiply-inherits from streambuf where compilers allow, allowing code such as the following:
wxTextCtrl *control = new wxTextCtrl(...);
ostream stream(control)
stream << 123.456 << " some text\n";
stream.flush();
If your compiler does not support derivation from streambuf and gives a compile error, define the symbol NO_TEXT_WINDOW_STREAM in the
wxTextCtrl header file.
Event handling
To process input from a text control, use these event handler macros to direct input to member functions that take a wxCommandEvent argument.
EVT_TEXT(id, func) | Respond to a wxEVT_COMMAND_TEXT_UPDATED event, generated when the text changes. |
EVT_TEXT_ENTER(id, func) | Respond to a wxEVT_COMMAND_TEXT_ENTER event, generated when enter is pressed in a single-line text control. |
wxTextCtrl::wxTextCtrl
wxTextCtrl::~wxTextCtrl
wxTextCtrl::Clear
wxTextCtrl::Copy
wxTextCtrl::Create
wxTextCtrl::Cut
wxTextCtrl::DiscardEdits
wxTextCtrl::GetInsertionPoint
wxTextCtrl::GetLastPosition
wxTextCtrl::GetLineLength
wxTextCtrl::GetLineText
wxTextCtrl::GetNumberOfLines
wxTextCtrl::GetValue
wxTextCtrl::IsModified
wxTextCtrl::LoadFile
wxTextCtrl::OnChar
wxTextCtrl::OnDropFiles
wxTextCtrl::Paste
wxTextCtrl::PositionToXY
wxTextCtrl::Remove
wxTextCtrl::Replace
wxTextCtrl::SaveFile
wxTextCtrl::SetEditable
wxTextCtrl::SetInsertionPoint
wxTextCtrl::SetInsertionPointEnd
wxTextCtrl::SetSelection
wxTextCtrl::SetValue
wxTextCtrl::ShowPosition
wxTextCtrl::WriteText
wxTextCtrl::AppendText
wxTextCtrl::XYToPosition
wxTextCtrl::operator <<
wxTextCtrl()
Default constructor.
wxTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator, const wxString& name = "text")
Constructor, creating and showing a text control.
Parameters
parent
id
value
pos
size
style
validator
name
Remarks
The horizontal scrollbar (wxTE_HSCROLL style flag) will only be created for multi-line text controls. Without a horizontal scrollbar, text lines that don't fit in the control's size will be wrapped (but no newline character is inserted). Single line controls don't have a horizontal scrollbar, the text is automatically scrolled so that the insertion point is always visible.
Under Windows, if the wxTE_MULTILINE style is used, the window is implemented as a Windows rich text control with unlimited capacity. Otherwise, normal edit control limits apply.
See also
wxTextCtrl::Create, wxValidator
~wxTextCtrl()
Destructor, destroying the text control.
virtual void Clear()
Clears the text in the control.
virtual void Copy()
Copies the selected text to the clipboard under Motif and MS Windows.
bool Create(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator, const wxString& name = "text")
Creates the text control for two-step construction. Derived classes should call or replace this function. See wxTextCtrl::wxTextCtrl for further details.
virtual void Cut()
Copies the selected text to the clipboard and removes the selection.
void DiscardEdits()
Resets the internal 'modified' flag as if the current edits had been saved.
virtual long GetInsertionPoint() const
Returns the insertion point. This is defined as the zero based index of the character position to the right of the insertion point. For example, if the insertion point is at the end of the text control, it is equal to both GetValue().Length() and GetLastPosition().
The following code snippet safely returns the character at the insertion point or the zero character if the point is at the end of the control.
char GetCurrentChar(wxTextCtrl *tc) {
if (tc->GetInsertionPoint() == tc->GetLastPosition())
return '\0';
return tc->GetValue[tc->GetInsertionPoint()];
}
virtual long GetLastPosition() const
Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control.
int GetLineLength(long lineNo) const
Gets the length of the specified line, not including any trailing newline character(s).
Parameters
lineNo
Return value
The length of the line, or -1 if lineNo was invalid.
wxString GetLineText(long lineNo) const
Returns the contents of a given line in the text control, not including any trailing newline character(s).
Parameters
lineNo
Return value
The contents of the line.
int GetNumberOfLines() const
Returns the number of lines in the text control buffer.
Remarks
Note that even empty text controls have one line (where the insertion point is), so GetNumberOfLines() never returns 0.
For gtk_text (multi-line) controls, the number of lines is calculated by actually counting newline characters in the buffer. You may wish to avoid using functions that work with line numbers if you are working with controls that contain large amounts of text.
wxString GetValue() const
Gets the contents of the control.
bool IsModified() const
Returns TRUE if the text has been modified.
bool LoadFile(const wxString& filename)
Loads and displays the named file, if it exists.
Parameters
filename
Return value
TRUE if successful, FALSE otherwise.
void OnChar(wxKeyEvent& event)
Default handler for character input.
Remarks
It is possible to intercept character input by overriding this member. Call this function to let the default behaviour take place; not calling it results in the character being ignored. You can replace the keyCode member of event to translate keystrokes.
Note that Windows and Motif have different ways of implementing the default behaviour. In Windows, calling wxTextCtrl::OnChar immediately processes the character. In Motif, calling this function simply sets a flag to let default processing happen. This might affect the way in which you write your OnChar function on different platforms.
See also
void OnDropFiles(wxDropFilesEvent& event)
This event handler function implements default drag and drop behaviour, which is to load the first dropped file into the control.
Parameters
event
Remarks
This is not yet implemented for the GTK.
See also
virtual void Paste()
Pastes text from the clipboard to the text item.
long PositionToXY(long pos, long *x, long *y) const
Converts given position to a zero-based column, line number pair.
Parameters
pos
x
y
Return value
Non-zero on success, zero on failure (most likely due to a too large position parameter).
See also
wxPython note:
In Python, PositionToXY() returns a tuple containing the x and
y values, so (x,y) = PositionToXY() is equivalent to the call described
above.
virtual void Remove(long from, long to)
Removes the text starting at the first given position up to (but not including) the character at the last position.
Parameters
from
to
virtual void Replace(long from, long to, const wxString& value)
Replaces the text starting at the first position up to (but not including) the character at the last position with the given text.
Parameters
from
to
value
bool SaveFile(const wxString& filename)
Saves the contents of the control in a text file.
Parameters
filename
Return value
TRUE if the operation was successful, FALSE otherwise.
virtual void SetEditable(const bool editable)
Makes the text item editable or read-only, overriding the wxTE_READONLY flag.
Parameters
editable
virtual void SetInsertionPoint(long pos)
Sets the insertion point at the given position.
Parameters
pos
virtual void SetInsertionPointEnd()
Sets the insertion point at the end of the text control. This is equivalent to SetInsertionPoint(GetLastPosition()).
virtual void SetSelection(long from, long to)
Selects the text starting at the first position up to (but not including) the character at the last position.
Parameters
from
to
virtual void SetValue(const wxString& value)
Sets the text value.
Parameters
value
void ShowPosition(long pos)
Makes the line containing the given position visible.
Parameters
pos
void WriteText(const wxString& text)
Writes the text into the text control at the current insertion position.
Parameters
text
Remarks
Newlines in the text string are the only control characters allowed, and they will cause appropriate line breaks. See wxTextCtrl::<< and wxTextCtrl::AppendText for more convenient ways of writing to the window.
After the write operation, the insertion point will be at the end of the inserted text, so subsequent write operations will be appended. To append text after the user may have interacted with the control, call wxTextCtrl::SetInsertionPointEnd before writing.
void AppendText(const wxString& text)
Appends the text to the end of the text control.
Parameters
text
Remarks
After the text is appended, the insertion point will be at the end of the text control. If this behaviour is not desired, the programmer should use GetInsertionPoint and SetInsertionPoint.
See also
long XYToPosition(long x, long y)
Converts the given zero based column and line number to a position.
Parameters
x
y
Return value
The position value.
wxTextCtrl& operator <<(const wxString& s)
wxTextCtrl& operator <<(int i)
wxTextCtrl& operator <<(long i)
wxTextCtrl& operator <<(float f)
wxTextCtrl& operator <<(double d)
wxTextCtrl& operator <<(char c)
Operator definitions for appending to a text control, for example:
wxTextCtrl *wnd = new wxTextCtrl(my_frame); (*wnd) << "Welcome to text control number " << 1 << ".\n";