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 control will generate the message wxEVENT_TYPE_TEXT_ENTER_COMMAND (otherwise pressing <Enter> is either processed internally by the control or used for navigation between dialog controls). |
wxTE_PROCESS_TAB | The control will receieve EVT_CHAR messages for TAB pressed - normally, TAB is used for passing to the next control in a dialog instead. For the control created with this style, you can still use Ctrl-Enter to pass to the next control from the keyboard. |
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. No effect under GTK+. |
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.
Note that any use of C++ iostreams (including this one) deprecated and might get completely removed in the future.
Event handling
The following commands are processed by default event handlers in wxTextCtrl: wxID_CUT, wxID_COPY, wxID_PASTE, wxID_UNDO, wxID_REDO. The associated UI update events are also processed automatically, when the control has the focus.
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. Notice that this event will always be sent when the text controls contents changes - whether this is due to user input or comes from the program itself (for example, if SetValue() is called) |
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::AppendText
wxTextCtrl::CanCopy
wxTextCtrl::CanCut
wxTextCtrl::CanPaste
wxTextCtrl::CanRedo
wxTextCtrl::CanUndo
wxTextCtrl::Clear
wxTextCtrl::Copy
wxTextCtrl::Create
wxTextCtrl::Cut
wxTextCtrl::DiscardEdits
wxTextCtrl::GetInsertionPoint
wxTextCtrl::GetLastPosition
wxTextCtrl::GetLineLength
wxTextCtrl::GetLineText
wxTextCtrl::GetNumberOfLines
wxTextCtrl::GetSelection
wxTextCtrl::GetValue
wxTextCtrl::IsModified
wxTextCtrl::LoadFile
wxTextCtrl::OnChar
wxTextCtrl::OnDropFiles
wxTextCtrl::Paste
wxTextCtrl::PositionToXY
wxTextCtrl::Redo
wxTextCtrl::Remove
wxTextCtrl::Replace
wxTextCtrl::SaveFile
wxTextCtrl::SetEditable
wxTextCtrl::SetInsertionPoint
wxTextCtrl::SetInsertionPointEnd
wxTextCtrl::SetSelection
wxTextCtrl::SetValue
wxTextCtrl::ShowPosition
wxTextCtrl::Undo
wxTextCtrl::WriteText
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.
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
virtual bool CanCopy()
Returns TRUE if the selection can be copied to the clipboard.
virtual bool CanCut()
Returns TRUE if the selection can be cut to the clipboard.
virtual bool CanPaste()
Returns TRUE if the contents of the clipboard can be pasted into the text control. On some platforms (Motif, GTK) this is an approximation and returns TRUE if the control is editable, FALSE otherwise.
virtual bool CanRedo()
Returns TRUE if there is a redo facility available and the last operation can be redone.
virtual bool CanUndo()
Returns TRUE if there is an undo facility available and the last operation can be undone.
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.
virtual void GetSelection(long* from, long* to)
Gets the current selection span. If the returned values are equal, there was no selection.
Parameters
from
to
wxPython note: The wxPython version of this method returns a tuple consisting of the from and to values.
wxString GetValue() const
Gets the contents of the control. Notice that for a multiline text control, the lines will be separated by (Unix-style) \n characters, even under Windows where they are separated by a \r\n sequence in the native 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 implemented on non-Windows platforms.
See also
virtual void Paste()
Pastes text from the clipboard to the text item.
bool 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
TRUE on success, FALSE 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 Redo()
If there is a redo facility and the last operation can be redone, redoes the last operation. Does nothing if there is no redo facility.
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 and marks the control as not-modified.
Parameters
value
void ShowPosition(long pos)
Makes the line containing the given position visible.
Parameters
pos
virtual void Undo()
If there is an undo facility and the last operation can be undone, undoes the last operation. Does nothing if there is no undo facility.
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.
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";